benbjurstrom / laravel-sitemap-lite
Create and generate sitemaps with ease
Fund package maintenance!
spatie.be/open-source/support-us
Requires
- php: ^8.2||^8.3||^8.4
- illuminate/support: ^10.0|^11.0
- nesbot/carbon: ^2.71|^3.0
- spatie/laravel-package-tools: ^1.16.1
Requires (Dev)
- mockery/mockery: ^1.6.6
- orchestra/testbench: ^8.14|^9.0
- pestphp/pest: ^2.24
- spatie/pest-plugin-snapshots: ^2.1
- spatie/phpunit-snapshot-assertions: ^5.1.2
- spatie/temporary-directory: ^2.2
This package is not auto-updated.
Last update: 2025-01-05 22:40:24 UTC
README
A lightweight fork of spatie/laravel-sitemap designed for creating sitemaps manually without auto-discovery or crawling your site. The purpose of this fork is to lighten dependencies and code by removing the crawling functionality. If you need auto-discovery, please use the original package at spatie/laravel-sitemap.
You can create your sitemap manually:
use Carbon\Carbon; use Spatie\Sitemap\Sitemap; use Spatie\Sitemap\Tags\Url; Sitemap::create() ->add(Url::create('/home') ->setLastModificationDate(Carbon::yesterday()) ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY) ->setPriority(0.1)) ->add(...) ->writeToFile($path);
You can also use one of your available filesystem disks to write the sitemap to:
Sitemap::create() ->add(Url::create('/home')) ->writeToDisk('public', 'sitemap.xml');
You may need to set the file visibility on your sitemap. For example, if you are writing a sitemap to S3 that you want to be publicly available. You can set the third parameter to true
to make it public:
Sitemap::create() ->add(Url::create('/home')) ->writeToDisk('public', 'sitemap.xml', true);
You can also add your models directly by implementing the \Spatie\Sitemap\Contracts\Sitemapable
interface:
use Spatie\Sitemap\Contracts\Sitemapable; use Spatie\Sitemap\Tags\Url; class Post extends Model implements Sitemapable { public function toSitemapTag(): Url | string | array { return Url::create(route('blog.post.show', $this)) ->setLastModificationDate(Carbon::create($this->updated_at)) ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY) ->setPriority(0.1); } }
Now you can add a single post model to the sitemap or even a whole collection:
use Spatie\Sitemap\Sitemap; Sitemap::create() ->add($post) ->add(Post::all());
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
First, install the package via composer:
composer require benbjurstrom/laravel-sitemap-lite
The package will automatically register itself.
Usage
Adding alternates to links
Multilingual sites may have several alternate versions of the same page (one per language). Adding an alternate can be done as follows:
use Spatie\Sitemap\Sitemap; use Spatie\Sitemap\Tags\Url; Sitemap::create() ->add( Url::create('/extra-page') ->setPriority(0.5) ->addAlternate('/extra-pagina', 'nl') ) ->writeToFile($sitemapPath);
Note the addAlternate
function which takes an alternate URL and the locale it belongs to.
Adding images to links
URLs can also have images. See also https://developers.google.com/search/docs/advanced/sitemaps/image-sitemaps
use Spatie\Sitemap\Sitemap; use Spatie\Sitemap\Tags\Url; Sitemap::create() ->add( Url::create('https://example.com') ->addImage('https://example.com/images/home.jpg', 'Home page image') ) ->writeToFile($sitemapPath);
Adding videos to links
As well as images, videos can be wrapped by URL tags. See https://developers.google.com/search/docs/crawling-indexing/sitemaps/video-sitemaps
You can set required attributes like so:
use Spatie\Sitemap\Sitemap; use Spatie\Sitemap\Tags\Url; Sitemap::create() ->add( Url::create('https://example.com') ->addVideo('https://example.com/images/thumbnail.jpg', 'Video title', 'Video Description', 'https://example.com/videos/source.mp4', 'https://example.com/video/123') ) ->writeToFile($sitemapPath);
If you want to pass the optional parameters like family_friendly
, live
, or platform
:
use Spatie\Sitemap\Sitemap; use Spatie\Sitemap\Tags\Url; use Spatie\Sitemap\Tags\Video; $options = ['family_friendly' => Video::OPTION_YES, 'live' => Video::OPTION_NO]; $allowOptions = ['platform' => Video::OPTION_PLATFORM_MOBILE]; $denyOptions = ['restriction' => 'CA']; Sitemap::create() ->add( Url::create('https://example.com') ->addVideo('https://example.com/images/thumbnail.jpg', 'Video title', 'Video Description', 'https://example.com/videos/source.mp4', 'https://example.com/video/123', $options, $allowOptions, $denyOptions) ) ->writeToFile($sitemapPath);
Creating a sitemap index
You can create a sitemap index:
use Spatie\Sitemap\SitemapIndex; SitemapIndex::create() ->add('/pages_sitemap.xml') ->add('/posts_sitemap.xml') ->writeToFile($sitemapIndexPath);
You can pass a Spatie\Sitemap\Tags\Sitemap
object to manually set the lastModificationDate
property:
use Spatie\Sitemap\SitemapIndex; use Spatie\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>
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.
Credits
- Freek Van der Herten
- All Contributors
- Fork maintained by Ben Bjurstrom
License
The MIT License (MIT). Please see License File for more information.