2023-08-10 14:48:45 +02:00
# Refresh Sitemap
2023-08-09 15:20:41 +00:00
2026-10-14 17:32:12 +02:00
Generates and refreshes your `sitemap.xml` using a configuration file.
2023-08-09 15:20:41 +00:00
2026-10-14 17:32:12 +02:00
Supports localized routes out of the box with `DefaultRouteLocalizer` and optionally integrates with `mcamara/laravel-localization` .
## Requirements
- PHP `^8.5`
- Laravel / Illuminate `^13.0`
2024-09-25 16:28:32 +02:00
2023-08-10 14:48:45 +02:00
## Installation
2023-08-09 15:20:41 +00:00
2026-10-14 17:32:12 +02:00
Via Composer:
2023-08-09 15:20:41 +00:00
2026-10-14 17:32:12 +02:00
```bash
2024-09-25 16:28:32 +02:00
composer require uwil/sitemap-generator
2023-08-09 15:20:41 +00:00
```
## Usage
2026-10-14 17:32:12 +02:00
1. Publish the configuration file:
```bash
php artisan vendor:publish --tag=sitemap-generator
```
2026-10-15 14:54:37 +02:00
2. Adjust your settings in `config/sitemap-generator.php` (locales, routes, output storage disk & path, CRUD models).
2026-10-14 17:32:12 +02:00
3. Test your configuration in the console:
```bash
php artisan uwil:sitemap:refresh --dry-run
```
2023-08-09 15:20:41 +00:00
2026-10-14 17:32:12 +02:00
4. Generate the sitemap file:
2024-09-25 16:28:32 +02:00
2026-10-14 17:32:12 +02:00
```bash
php artisan uwil:sitemap:refresh
```
2024-09-25 16:28:32 +02:00
2026-10-15 16:31:18 +02:00
Or dispatch the generation to the queue:
```bash
php artisan uwil:sitemap:refresh --queue
```
2026-10-14 17:32:12 +02:00
A route is automatically registered to serve your generated sitemap:
`GET /sitemap.xml`
---
## Model Implementation
Models defined in the `cruds` config section must implement `Uwil\SitemapGenerator\Contracts\Sitemapable` .
You only need to define `getLocalizedUrl(string $locale)` to resolve the full URL for each configured locale. The package automatically generates the main URLs and their respective `xhtml:link` alternate (`hreflang` ) tags.
2024-09-25 16:28:32 +02:00
```php
2026-10-14 17:32:12 +02:00
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
2024-09-25 16:28:32 +02:00
use Spatie\Sitemap\Tags\Url;
2026-10-14 17:32:12 +02:00
use Uwil\SitemapGenerator\Contracts\Sitemapable;
class Product extends Model implements Sitemapable
{
/**
* Return the localized URL for a given locale.
*/
public function getLocalizedUrl(string $locale): string
{
// Example with mcamara/laravel-localization:
return LaravelLocalization::getURLFromRouteNameTranslated(
$locale,
'routes.product.show',
['product' => $this]
) ?? route('products.show', ['locale' => $locale, 'product' => $this]);
}
/**
* (Optional) Custom tag definition if using Spatie's native resolution.
*/
public function toSitemapTag(): Url|string|array
{
return route('products.show', $this);
}
}
2024-09-25 16:28:32 +02:00
```
2026-10-14 17:32:12 +02:00
---
2024-09-25 16:28:32 +02:00
2026-10-14 17:32:12 +02:00
## Configuration Example
2024-09-25 16:28:32 +02:00
2026-10-14 17:32:12 +02:00
In `config/sitemap-generator.php` :
2024-09-25 16:28:32 +02:00
```php
2026-10-14 17:32:12 +02:00
use App\Models\Product;
use Illuminate\Database\Eloquent\Builder;
return [
'single_pages' => [
'homepage',
'contact',
],
'cruds' => [
[
'model' => Product::class,
2026-10-15 12:04:03 +02:00
'model_filters' => [
2026-10-14 17:32:12 +02:00
['is_active', '=', true],
],
2026-10-15 12:04:03 +02:00
'relation_filters' => [
2026-10-15 14:54:37 +02:00
'category' => fn (Builder $query) => $query->where('published', true),
2026-10-14 17:32:12 +02:00
],
2026-10-15 12:04:03 +02:00
'allow_null_relation' => ['category'],
2026-10-14 17:32:12 +02:00
],
],
'supported_locales' => ['fr', 'en', 'es'],
/**
* Route localization strategy:
* - 'default' (or DefaultRouteLocalizer::class): uses Laravel's standard route names with {locale} parameter.
* - 'mcamara' (or McamaraRouteLocalizer::class): uses mcamara/laravel-localization route translations.
2026-10-15 14:54:37 +02:00
* - Custom class implementing RouteLocalizerInterface.
2026-10-14 17:32:12 +02:00
*/
'localizer' => 'default',
'schedule' => true,
'cron' => '45 15 * * *',
2026-10-15 14:54:37 +02:00
'output' => [
'storage_disk' => 'public',
'path' => 'sitemap.xml',
],
2026-10-14 17:32:12 +02:00
];
2024-09-25 16:28:32 +02:00
```
2023-08-09 15:20:41 +00:00
2026-10-14 17:32:12 +02:00
### CRUD Relation Filters & Nullable Relations
2026-10-15 12:04:03 +02:00
- **`model_filters` **: Standard Eloquent where clauses applied to the root model.
- **`relation_filters` **: Filters applied to model relations. Supports:
2026-10-14 17:32:12 +02:00
- `Closure` : `fn (Builder $q) => $q->where('status', true)`
- `array` : `['status' => true]`
- `string` : `'is_active'` (shorthand for `where('is_active', true)` )
2026-10-15 12:04:03 +02:00
- **`allow_null_relation` **: Array of relation names where models having `null` for this relation are still included in the sitemap (using `whereDoesntHave() OR whereHas()` ). Relations not listed here require the related model to exist.
2026-10-14 17:32:12 +02:00
2023-08-09 15:20:41 +00:00
2026-10-14 17:32:12 +02:00
---
## Security
2025-04-07 13:10:13 +02:00
2026-10-14 17:32:12 +02:00
If you discover any security-related issues, please email < equipe.web @ldc .fr > instead of using the issue tracker.
2025-04-07 13:10:13 +02:00
2026-10-14 17:32:12 +02:00
## Changelog
2025-04-07 13:10:13 +02:00
See the [CHANGELOG.md ](CHANGELOG.md ) file for details.
2026-10-14 17:32:12 +02:00
## Copyright and License
2025-04-07 13:10:13 +02:00
[Sitemap Generator ](https://lab.s-web.fr/ldc-web/projets/sitemap-generator ) was written by Uwil.
Copyright (c) 2023 and beyond Uwil.