1. Go to this page and download the library: Download skywalker-labs/sitemapper library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
skywalker-labs / sitemapper example snippets
use SkywalkerLabs\Sitemap\Sitemap;
public function sitemap(Sitemap $sitemap)
{
$sitemap->add('https://example.com/', now(), '1.0', 'daily');
$sitemap->add('https://example.com/about', now(), '0.8', 'monthly', images: [
['url' => 'https://example.com/img/about.jpg', 'title' => 'About Us']
]);
// Add more items as needed...
// Render XML using a view template
$items = $sitemap->getModel()->getItems();
return response()->view('sitemap.xml', compact('items'), 200, ['Content-Type' => 'application/xml']);
}
use SkywalkerLabs\Sitemap\Sitemap;
use Symfony\Component\HttpFoundation\Response;
class SitemapController
{
public function sitemap(): Response
{
$sitemap = new Sitemap();
$sitemap->add('https://example.com/', (new \DateTime())->format(DATE_ATOM), '1.0', 'daily');
$sitemap->add('https://example.com/contact', (new \DateTime())->format(DATE_ATOM), '0.5', 'monthly');
// Add more items as needed...
// Render XML
$xml = $sitemap->renderXml();
return new Response($xml, 200, ['Content-Type' => 'application/xml']);
}
}
use SkywalkerLabs\Sitemap\Config\SitemapConfig;
$config = (new SitemapConfig())
->setEscaping(true)
->setStrictMode(true)
->setUseGzip(true)
->setDefaultFormat('xml');
$sitemap = new Sitemap($config);
$config = new SitemapConfig(strictMode: true);
$sitemap = new Sitemap($config);
// Valid data works fine
$sitemap->add('https://example.com', '2023-12-01', '0.8', 'daily');
// Invalid data throws InvalidArgumentException
try {
$sitemap->add('not-a-url', '2023-12-01', '2.0', 'sometimes');
} catch (\InvalidArgumentException $e) {
echo "Validation error: " . $e->getMessage();
}
$sitemap = new Sitemap();
$sitemap->add('https://example.com/', date('c'), '1.0', 'daily');
// Render as XML
$xml = $sitemap->render('xml');
// Render as HTML
$html = $sitemap->render('html');
// Render as plain text
$txt = $sitemap->render('txt');
// Save to file
$sitemap->store('xml', 'sitemap', './public');
$sitemap = new Sitemap();
$sitemap->add('https://example.com/', date('c'), '1.0', 'daily');
$xml = $sitemap->renderXml(); // Returns XML string
$sitemap = new Sitemap();
$sitemap->add('https://example.com/', date('c'), '1.0', 'daily');
// Get the data for your view
$items = $sitemap->getModel()->getItems();
// Laravel: Use response()->view() or view()->render()
return response()->view('sitemap.xml', compact('items'), 200, ['Content-Type' => 'application/xml']);
// Symfony: Use Twig templates
return $this->render('sitemap.xml.twig', ['items' => $items], new Response('', 200, ['Content-Type' => 'application/xml']));
// Generic PHP: Include view templates
ob_start();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.