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();

$paginated_active_categories = Category::where('active', true)
    ->orderBy('desc', 'id')
    ->paginate();

return SimpleSitemap::fromEloquentCollection($paginated_active_categories);

// app/Http/Kernel.php

// ...
protected $middlewareAliases = [
    // ...
    'sitemap' => \SebaCarrasco93\SimpleSitemap\Middleware\SimpleSitemap::class,
];

// web.php or equivalent

Route::get('your-route', [YourController::class])
    ->middleware('sitemap'); //  👈

Route::get('your-route', function () {
    return 'It works with a closure, too';
})->middleware('sitemap'); // 👈

// app/Providers/RouteServiceProvider.php
Route::middleware(['web', 'sitemap']) // 👈
    ->group(base_path('routes/web.php'));

// web.php or equivalent

Route::get('to_exclude', function () {
    // You very important route to exclude
})->middleware('sitemap:exclude');

return SimpleSitemap::routes();

$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);

$table->addColumn('string', 'frequency')->nullable();
$table->addColumn('string', 'priority')->nullable();

// app/Models/YourModel.php

public function getFrequencyAttribute(?string $frequency = null): string
{
    return $frequency ?? 'weekly';
}

// app/Models/YourModel.php

public function getPriorityAttribute(?string $priority = null): string
{
    return $priority ?? '0.1';
}
bash
php artisan vendor:publish --tag="simple-sitemap-config"