Image

Sitemaper

A very flexible PHP sitemap generator library to suit all your sitemap needs

Sitemap PHP Sitemap Generator

Generating Sitemaps

There are two ways you can add sitemap items, via constructor or via the specified class method

Via class method

use Alexecus\Sitemaper\Sitemap;

$sitemap = new Sitemap('http://mysite.com');

$sitemap
    ->addItem('/', [
        'lastmod' => '2020-05-15',
        'changefreq' => 'monthly',
        'priority' => '1.0',
    ])
    ->addItem('/page', [
        'lastmod' => '2020-05-15',
        'changefreq' => 'daily',
        'priority' => '0.8',
    ])
    ->addItem('/page/item', [
        'lastmod' => '2020-05-15',
        'changefreq' => 'daily',
        'priority' => '0.8',
    ]);

Via constructor

use Alexecus\Sitemaper\Sitemap;

$sitemap = new Sitemap('http://mysite.com', [
    '/' => [
        'lastmod' => '2020-05-15',
        'changefreq' => 'monthly',
        'priority' => '1.0',
    ],
    '/page' => [
        'lastmod' => '2020-05-15',
        'changefreq' => 'daily',
        'priority' => '0.8',
    ],
    '/page/item' => [
        'lastmod' => '2020-05-15',
        'changefreq' => 'daily',
        'priority' => '0.8',
    ]
]);

After adding items to the sitemap instance, you can now write the actual sitemap to a file. To dump your sitemap to a file, call the write method.

The write method accepts two arguments

  • Filepath (Required) - The filepath where the sitemap will be written
  • Format (Optional) - The format of the sitemap that will be written. Defaults to XML
$sitemap->write('sitemap.xml');

The example above will product the following sitemap

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://mysite.com/</loc>
    <lastmod>2020-05-15</lastmod>
    <changefreq>monthly</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://mysite.com/page</loc>
    <lastmod>2020-05-15</lastmod>
    <changefreq>daiy</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc>http://mysite.com/page/item</loc>
    <lastmod>2020-05-15</lastmod>
    <changefreq>daiy</changefreq>
    <priority>0.8</priority>
  </url>
</urlset>