sebacarrasco93/laravel-simple-sitemap

Laravel Simple Sitemap

1.0.1 2024-03-07 17:30 UTC

This package is auto-updated.

Last update: 2024-11-08 04:32:45 UTC


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.