A very simple package: Create sitemaps "on the fly"
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',
];
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());
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);
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.