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