mfonte / laravel-sitemap
Laravel Sitemap Generator. Supports SitemapIndex, Images Sitemap and News Sitemap.
Requires
- ext-openssl: >1.0.2
- illuminate/contracts: ^9.0 || ^10.0
- illuminate/support: ^9.0 || ^10.0
- nesbot/carbon: ^2.0
- spatie/laravel-package-tools: ^1.14
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- mockery/mockery: ^1.3.3
- orchestra/testbench: ^7.0
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.5
- spatie/phpunit-snapshot-assertions: ^4.2
- spatie/temporary-directory: ^2.1
This package is auto-updated.
Last update: 2024-11-30 14:23:43 UTC
README
This package has been forked from spatie/laravel-sitemap, to remove support for SitemapGenerator
, remove installation requirement for PHP 8, and add support for Images Sitemaps and News Sitemaps.
Additionally, this package adds support for installation outside a Laravel Project. See Using this package outside Laravel section.
This package can generate a valid sitemap by writing your own custom logic for the sitemap structure, via the API provided by this package.
Heads up! This package requires Laravel 9 or Laravel 10 For PHP 7.4 and Laravel 8 compatibility refer to v1.1.*
Installation
For Laravel 9 or 10, or for a non-Laravel-based project running on PHP >= 8.0
composer require mfonte/laravel-sitemap
For Laravel 8, or for a non-Laravel-based project running on PHP >= 7.4 && < 8.0
composer require mfonte/laravel-sitemap "^1.1"
Creating sitemaps
You can only create your sitemap manually:
use Carbon\Carbon; use Mfonte\Sitemap\Sitemap; use Mfonte\Sitemap\Tags\Url; Sitemap::create() ->add( Url::create('/home') ->setLastModificationDate(Carbon::yesterday()) ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY) ->setPriority(0.1) ->addImage('/path/to/image', 'A wonderful Caption') ->addNews('A long story short', 'en', Carbon::yesterday(), 'Sitemaps are this great!') ) ->add(...) ->writeToFile($path);
The sitemap generator can automatically understand what type of items you placed inside the sitemap, and create a valid schema accordingly. This is an example of a sitemap header with images and news:
<?xml version="1.0" encoding="UTF-8"?>\n <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"> <url> <loc>http://localhost/page10</loc>\n <lastmod>2016-01-01T00:00:00+00:00</lastmod> <changefreq>daily</changefreq> <priority>0.8</priority> <image:image> <image:loc>http://localhost/imageUrl</image:loc> </image:image> <news:news> <news:publication_date>2015-12-29</news:publication_date> <news:title>defaultTitle</news:title> <news:publication> <news:name>defaultName</news:name> <news:language>defaultLanguage</news:language> </news:publication> </news:news> </url> </urlset>
You can also add your models directly by implementing the \Mfonte\Sitemap\Contracts\Sitemapable
interface.
use Mfonte\Sitemap\Contracts\Sitemapable; use Mfonte\Sitemap\Tags\Url; class Post extends Model implements Sitemapable { public function toSitemapTag(): Url | string | array { return route('blog.post.show', $this); } }
Now you can add a single post model to the sitemap or even a whole collection.
use Mfonte\Sitemap\Sitemap; Sitemap::create() ->add($post) ->add(Post::all());
This way you can add all your pages super fast without the need to crawl them all.
Creating a sitemap index
You can create a sitemap index:
use Mfonte\Sitemap\SitemapIndex; SitemapIndex::create() ->add('/pages_sitemap.xml') ->add('/posts_sitemap.xml') ->writeToFile($sitemapIndexPath);
You can pass a Mfonte\Sitemap\Tags\Sitemap
object to manually set the lastModificationDate
property.
use Mfonte\Sitemap\SitemapIndex; use Mfonte\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>
Usage outside of Laravel
The same instructions above apply, except for:
- You can not use
Contracts\Sitemapable
to extend a Model (you're not on Laravel, aren't you?) - You have to use the
Sitemap::render()
,Sitemap::writeToFile()
,SitemapIndex::render()
andSitemapIndex::writeToFile()
via providing the extra boolean flag$nativeRenderer = true
- You can not use
Sitemap::writeToDisk()
,Sitemap::toResponse()
,SitemapIndex::writeToDisk()
andSitemapIndex::toResponse()
So, for example, a basic approach may be:
use Carbon\Carbon; use Mfonte\Sitemap\Sitemap; use Mfonte\Sitemap\Tags\Url; $sitemapStream = Sitemap::create()->add( Url::create('/home') ->setLastModificationDate(Carbon::yesterday()) ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY) ->setPriority(0.1) ->addImage('/path/to/image', 'A wonderful Caption') ->addNews('A long story short', 'en', Carbon::yesterday(), 'Sitemaps are this great!') ) ->add(...) ->render(nativeRenderer: true);
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Credits
License
The MIT License (MIT). Please see License File for more information. This package has been forked from https://github.com/spatie/laravel-sitemap and the relative license file has been migrated into this repository as-it-is.