sebacarrasco93 / laravel-simple-sitemap
Laravel Simple Sitemap
Fund package maintenance!
Seba Carrasco Poblete
Requires
- php: ^8.1
- illuminate/contracts: ^10.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- larastan/larastan: ^2.0.1
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.8
- orchestra/testbench: ^8.8
- pestphp/pest: ^2.20
- pestphp/pest-plugin-arch: ^2.5
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
README
A very simple package: Create sitemaps "on the fly"
Installation
You can install the package via composer:
composer require sebacarrasco93/laravel-simple-sitemap
You can publish the config file with:
php artisan vendor:publish --tag="simple-sitemap-config"
This is the contents of the published config file:
return [ 'default_frequency' => 'monthly', 'default_priority' => '0.50', ];
Usage
Create a sitemap for Eloquent Collections
Let's assume that you want to make a sitemap of all the categories, you can do that in only 3 steps!
// app/Models/Category use SebaCarrasco93\SimpleSitemap\Traits\SimpleSitemapCollection; // 👈 1: Import Trait class Category extends Model { use HasFactory; // ... use SimpleSitemapCollection; // 👈 2: Use the trait // ... // 👇 Step 3: Create getSitemapUrlAttribute() method and specify the full url public function getSitemapUrlAttribute(): string { return route('category.show', $this); } }
Now, you can use it
// web.php, controller or equivalent $categories = Category::get(); return SimpleSitemap::fromEloquentCollection($categories);
Can I short the syntax? Of course!
return Category::sitemap(); // Equivalent to SimpleSitemap::fromEloquentCollection(Category::get());
Advanced usage
A sitemap for only active categories? Sure!
return Category::where('active', true) ->sitemap();
A sitemap for active, and only 10 last categories? It's Eloquent and Laravel!
$active_categories = Category::where('active', true) ->orderBy('desc', 'id')->take(10)->get(); return SimpleSitemap::fromCollection($active_categories);
Easy Peasy!
Optionally, you can create a index sitemap with your sitemap collections
$routes = [ route('sitemaps/index-1'), // You can pass it as a route 'https://yourdomain.com/sitemaps/index-2', // or, as full path '/sitemaps/index-3', // as a relative path, too ]; return SimpleSitemap::index($routes);
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.