PHP code example of sebacarrasco93 / laravel-simple-sitemap

1. Go to this page and download the library: Download sebacarrasco93/laravel-simple-sitemap library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sebacarrasco93 / laravel-simple-sitemap example snippets


return [    

    'default_frequency' => 'monthly',
    
    'default_priority' => '0.50',
];

// 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);
    }
}


// web.php, controller or equivalent

$categories = Category::get();

return SimpleSitemap::fromEloquentCollection($categories);

return Category::sitemap(); // Equivalent to SimpleSitemap::fromEloquentCollection(Category::get());

return Category::where('active', true)
    ->sitemap();

$active_categories = Category::where('active', true)
    ->orderBy('desc', 'id')->take(10)->get();

return SimpleSitemap::fromCollection($active_categories);

$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);
bash
php artisan vendor:publish --tag="simple-sitemap-config"