finalstrike/sitemap

Create and generate sitemaps with ease

dev-main 2022-08-16 15:41 UTC

This package is auto-updated.

Last update: 2024-05-16 19:28:00 UTC


README

use Carbon\Carbon;
use FinalStrike\Sitemap\Sitemap;
use FinalStrike\Sitemap\Tags\Url;

Sitemap::create()

    ->add(Url::create('/home')
        ->setLastModificationDate(Carbon::yesterday())
        ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
        ->setPriority(0.1))

   ->add(...)

   ->writeToFile($path);

Installation

First, install the package via composer:

composer require finalstrike/sitemap

The package will automatically register itself.

Configuration

You can override the views. First publish the configuration:

php artisan vendor:publish --provider="FinalStrike\Sitemap\SitemapServiceProvider"

Usage

use Carbon\Carbon;

Sitemap::create()
   ->add('/page1')
   ->add('/page2')
   ->add(Url::create('/page3')->setLastModificationDate(Carbon::create('2016', '1', '1')))
   ->writeToFile($sitemapPath);

Creating a sitemap index

You can create a sitemap index:

use FinalStrike\Sitemap\SitemapIndex;

SitemapIndex::create()
    ->add('/pages_sitemap.xml')
    ->add('/posts_sitemap.xml')
    ->writeToFile($sitemapIndexPath);

You can pass a FinalStrike\Sitemap\Tags\Sitemap object to manually set the lastModificationDate property.

use FinalStrike\Sitemap\SitemapIndex;
use FinalStrike\Sitemap\Tags\Sitemap;

SitemapIndex::create()
    ->add('/pages_sitemap.xml')
    ->add(Sitemap::create('/posts_sitemap.xml')
        ->setLastModificationDate(Carbon::yesterday()))
    ->writeToFile($sitemapIndexPath);

the generated sitemap index will look similar to this:

<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <sitemap>
      <loc>http://www.example.com/pages_sitemap.xml</loc>
      <lastmod>2016-01-01T00:00:00+00:00</lastmod>
   </sitemap>
   <sitemap>
      <loc>http://www.example.com/posts_sitemap.xml</loc>
      <lastmod>2015-12-31T00:00:00+00:00</lastmod>
   </sitemap>
</sitemapindex>

License

The MIT License (MIT). Please see License File for more information.