feat: :sparkles add sitemap-generator package

This commit is contained in:
Corentin Huet 2026-10-27 16:38:54 +01:00
parent 3d580971c2
commit 5aaea9e301
10 changed files with 643 additions and 135 deletions

2
.gitignore vendored
View file

@ -2,7 +2,7 @@
/public/hot
/public/storage
/public/
!/public/sitemap.xml
/public/sitemap.xml
/storage/*.key
/vendor
.env

View file

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace App\Lib\Sitemap;
use Illuminate\Support\Facades\Route;
use League\Uri\Contracts\UriInterface;
use League\Uri\Uri;
use Uwil\SitemapGenerator\Contracts\RouteLocalizerInterface;
final readonly class PrefixedRouteLocalizer implements RouteLocalizerInterface
{
private const string DEFAULT_LOCALE = 'fr';
public function canBeUsed(): bool
{
return true;
}
public function getLocalizedUrl(string $locale, string $routeName): ?UriInterface
{
$name = $locale === self::DEFAULT_LOCALE
? $routeName
: $locale . '.' . $routeName;
if (! Route::has($name)) {
return null;
}
return Uri::new(route($name));
}
}

View file

@ -6,9 +6,12 @@ use App\Lib\Utils;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
use Spatie\Sitemap\Tags\Url;
use Spatie\Translatable\HasTranslations;
use Uwil\SitemapGenerator\Contracts\Sitemapable;
use Uwil\SitemapGenerator\SitemapHelper;
class Gamme extends Model
class Gamme extends Model implements Sitemapable
{
use HasFactory;
use HasTranslations;
@ -172,4 +175,29 @@ class Gamme extends Model
{
return $this->hasMany(Product::class);
}
/**
* Return the localized URL for a given locale.
*/
public function getLocalizedUrl(string $locale): string
{
$routeName = $locale === 'fr' ? 'gammes.show' : $locale . '.gammes.show';
return route($routeName, [
'gammeSlug' => $this->getTranslation('slug', $locale),
]);
}
/**
* toSitemapTag method
*
* @return Url|string|array
*/
public function toSitemapTag(): Url | string | array
{
$model = $this;
$route = 'gammes.show';
$params = ['publishedProduct' => $model];
return SitemapHelper::generateUrl($route, $params, $model);
}
}

View file

@ -6,9 +6,12 @@ use App\Lib\Utils;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
use Spatie\Sitemap\Tags\Url;
use Spatie\Translatable\HasTranslations;
use Uwil\SitemapGenerator\Contracts\Sitemapable;
use Uwil\SitemapGenerator\SitemapHelper;
class Product extends Model
class Product extends Model implements Sitemapable
{
use HasFactory;
use HasTranslations;
@ -105,4 +108,30 @@ class Product extends Model
{
return $this->belongsTo(Gamme::class);
}
/**
* Return the localized URL for a given locale.
*/
public function getLocalizedUrl(string $locale): string
{
$routeName = $locale === 'fr' ? 'product.show' : $locale . '.product.show';
return route($routeName, [
'gammeSlug' => $this->gamme->getTranslation('slug', $locale),
'productSlug' => $this->getTranslation('slug', $locale),
]);
}
/**
* toSitemapTag method
*
* @return Url|string|array
*/
public function toSitemapTag(): Url | string | array
{
$model = $this;
$route = 'gammes.show';
$params = ['publishedProduct' => $model];
return SitemapHelper::generateUrl($route, $params, $model);
}
}

View file

@ -2,11 +2,23 @@
namespace App\Providers;
use App\Lib\Sitemap\PrefixedRouteLocalizer;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;
use Uwil\SitemapGenerator\Localizers\DefaultRouteLocalizer;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(DefaultRouteLocalizer::class, PrefixedRouteLocalizer::class);
}
/**
* Bootstrap any application services.
*

View file

@ -19,7 +19,8 @@
"mews/purifier": "^3.3",
"sentry/sentry-laravel": "^4.1",
"spatie/laravel-translatable": "^6.0",
"uwil/laravel-k8s": "*"
"uwil/laravel-k8s": "*",
"uwil/sitemap-generator": "^8.0"
},
"require-dev": {
"driftingly/rector-laravel": "^2.5",

385
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "ffcb3d9c1af884d4bf7da3051c30429b",
"content-hash": "34983c0189d28a600f3bcbb5f9b6c104",
"packages": [
{
"name": "brick/math",
@ -4281,6 +4281,86 @@
],
"time": "2026-10-08T12:31:48+00:00"
},
{
"name": "spatie/crawler",
"version": "9.4.2",
"source": {
"type": "git",
"url": "https://github.com/spatie/crawler.git",
"reference": "937bcf5786e66bca20da50db004f6d1443e81418"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/crawler/zipball/937bcf5786e66bca20da50db004f6d1443e81418",
"reference": "937bcf5786e66bca20da50db004f6d1443e81418",
"shasum": ""
},
"require": {
"guzzlehttp/guzzle": "^7.8.2 || ^8.0",
"guzzlehttp/psr7": "^2.6.3 || ^3.0",
"php": "^8.4",
"psr/http-client": "^1.0",
"spatie/robots-txt": "^2.0",
"symfony/css-selector": "^7.0|^8.0",
"symfony/dom-crawler": "^7.0|^8.0"
},
"require-dev": {
"pestphp/pest": "^4.0",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan": "^2.0",
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-phpunit": "^2.0",
"spatie/invade": "^2.1",
"spatie/ray": "^1.37"
},
"suggest": {
"spatie/browsershot": "Required for JavaScript rendering with BrowsershotRenderer (^5.0.5)"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-v9": "9.x-dev"
}
},
"autoload": {
"psr-4": {
"Spatie\\Crawler\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be"
}
],
"description": "Crawl all internal links found on a website",
"homepage": "https://github.com/spatie/crawler",
"keywords": [
"crawler",
"link",
"spatie",
"website"
],
"support": {
"issues": "https://github.com/spatie/crawler/issues",
"source": "https://github.com/spatie/crawler/tree/9.4.2"
},
"funding": [
{
"url": "https://spatie.be/open-source/support-us",
"type": "custom"
},
{
"url": "https://github.com/spatie",
"type": "github"
}
],
"time": "2026-08-07T07:19:51+00:00"
},
{
"name": "spatie/laravel-package-tools",
"version": "1.93.3",
@ -4342,6 +4422,80 @@
],
"time": "2026-09-28T07:16:50+00:00"
},
{
"name": "spatie/laravel-sitemap",
"version": "8.2.0",
"source": {
"type": "git",
"url": "https://github.com/spatie/laravel-sitemap.git",
"reference": "51f52030ab22abf5e95ed037fea5f8073114e784"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-sitemap/zipball/51f52030ab22abf5e95ed037fea5f8073114e784",
"reference": "51f52030ab22abf5e95ed037fea5f8073114e784",
"shasum": ""
},
"require": {
"illuminate/support": "^12.0|^13.0",
"nesbot/carbon": "^2.71|^3.0",
"php": "^8.4",
"spatie/crawler": "^9.0",
"spatie/laravel-package-tools": "^1.16.1"
},
"require-dev": {
"larastan/larastan": "^3.0",
"laravel/pint": "^1.13",
"orchestra/testbench": "^10.0|^11.0",
"pestphp/pest": "^4.0",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-phpunit": "^2.0",
"spatie/pest-plugin-snapshots": "^2.1",
"spatie/temporary-directory": "^2.2"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Spatie\\Sitemap\\SitemapServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Spatie\\Sitemap\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
}
],
"description": "Create and generate sitemaps with ease",
"homepage": "https://github.com/spatie/laravel-sitemap",
"keywords": [
"laravel-sitemap",
"spatie"
],
"support": {
"source": "https://github.com/spatie/laravel-sitemap/tree/8.2.0"
},
"funding": [
{
"url": "https://spatie.be/open-source/support-us",
"type": "custom"
}
],
"time": "2026-06-24T07:59:14+00:00"
},
{
"name": "spatie/laravel-translatable",
"version": "6.15.0",
@ -4426,6 +4580,66 @@
],
"time": "2026-10-19T00:09:37+00:00"
},
{
"name": "spatie/robots-txt",
"version": "2.5.5",
"source": {
"type": "git",
"url": "https://github.com/spatie/robots-txt.git",
"reference": "b451e56b67ab9c852d354de75e88e3b797729b9e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/robots-txt/zipball/b451e56b67ab9c852d354de75e88e3b797729b9e",
"reference": "b451e56b67ab9c852d354de75e88e3b797729b9e",
"shasum": ""
},
"require": {
"php": "^8.1"
},
"require-dev": {
"phpunit/phpunit": "^11.5.2"
},
"type": "library",
"autoload": {
"psr-4": {
"Spatie\\Robots\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Brent Roose",
"email": "brent@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
}
],
"description": "Determine if a page may be crawled from robots.txt and robots meta tags",
"homepage": "https://github.com/spatie/robots-txt",
"keywords": [
"robots-txt",
"spatie"
],
"support": {
"issues": "https://github.com/spatie/robots-txt/issues",
"source": "https://github.com/spatie/robots-txt/tree/2.5.5"
},
"funding": [
{
"url": "https://spatie.be/open-source/support-us",
"type": "custom"
},
{
"url": "https://github.com/spatie",
"type": "github"
}
],
"time": "2026-10-17T08:57:09+00:00"
},
{
"name": "symfony/clock",
"version": "v8.1.9",
@ -4743,6 +4957,76 @@
],
"time": "2026-06-05T06:23:12+00:00"
},
{
"name": "symfony/dom-crawler",
"version": "v8.1.5",
"source": {
"type": "git",
"url": "https://github.com/symfony/dom-crawler.git",
"reference": "94f70e1b2e5b7b8f8618871fd9eaad3e39e9f03d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/dom-crawler/zipball/94f70e1b2e5b7b8f8618871fd9eaad3e39e9f03d",
"reference": "94f70e1b2e5b7b8f8618871fd9eaad3e39e9f03d",
"shasum": ""
},
"require": {
"php": ">=8.4.1",
"symfony/polyfill-ctype": "^1.8",
"symfony/polyfill-mbstring": "^1.0"
},
"require-dev": {
"symfony/css-selector": "^7.4|^8.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Symfony\\Component\\DomCrawler\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Eases DOM navigation for HTML and XML documents",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/dom-crawler/tree/v8.1.5"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://github.com/nicolas-grekas",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2026-08-21T17:47:34+00:00"
},
{
"name": "symfony/error-handler",
"version": "v8.1.8",
@ -7275,6 +7559,105 @@
"issues": "https://lab.s-web.fr/ldc-web/projets/laravel-k8s/-/issues"
}
},
{
"name": "uwil/sitemap-generator",
"version": "8.0.1",
"source": {
"type": "git",
"url": "git@lab.s-web.fr:ldc-web/projets/sitemap-generator.git",
"reference": "bab3583abd3be0b780df9ca1540b2207b587e18f"
},
"dist": {
"type": "zip",
"url": "https://lab.s-web.fr/api/v4/projects/108/packages/composer/archives/uwil/sitemap-generator.zip?sha=bab3583abd3be0b780df9ca1540b2207b587e18f",
"reference": "bab3583abd3be0b780df9ca1540b2207b587e18f",
"shasum": ""
},
"require": {
"illuminate/support": "^13.0",
"laravel/framework": "^13.0",
"php": "^8.5",
"spatie/laravel-sitemap": "^8.1"
},
"require-dev": {
"ace-of-aces/intellipest": "^1.0",
"larastan/larastan": "^3.0",
"laravel/pint": "^1.32",
"mcamara/laravel-localization": "^2.4",
"orchestra/testbench": "^11.3",
"pestphp/pest": "^5.3",
"php-parallel-lint/php-parallel-lint": "^1.4",
"slevomat/coding-standard": "^8.15"
},
"suggest": {
"league/flysystem-aws-s3-v3": "Required to store the sitemap on an Amazon S3 disk",
"mcamara/laravel-localization": "Required for localized route name translations"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Uwil\\SitemapGenerator\\SitemapGeneratorServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Uwil\\SitemapGenerator\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Uwil\\SitemapGenerator\\Tests\\": "tests/"
}
},
"scripts": {
"lint": [
"./vendor/bin/phpcs && ./vendor/bin/pint --test"
],
"test": [
"./vendor/bin/pest"
],
"format": [
"./vendor/bin/pint"
],
"analyze": [
"./vendor/bin/phpstan analyse"
],
"lint:fix": [
"./vendor/bin/phpcbf && ./vendor/bin/pint"
],
"lint:php": [
"./vendor/bin/parallel-lint --exclude vendor ."
],
"lint:pint": [
"./vendor/bin/pint --test"
],
"lint:phpcs": [
"./vendor/bin/phpcs"
],
"lint:phpstan": [
"./vendor/bin/phpstan analyse"
],
"test:coverage": [
"./vendor/bin/pest --log-junit=junit.xml --coverage --coverage-html=tests/coverage --coverage-clover=coverage.xml"
]
},
"license": [
"proprietary"
],
"authors": [
{
"name": "LDC Digital",
"email": "digital@ldc.com"
}
],
"description": "Create sitemap.xml",
"keywords": [
"laravel",
"sitemap"
]
},
{
"name": "vlucas/phpdotenv",
"version": "v5.8.0",

View file

@ -0,0 +1,99 @@
<?php
declare(strict_types=1);
return [
/**
* Single route names
*
* Ex:
*
* 'single_pages' => [
* 'homepage',
* ],
*/
'single_pages' => [
'home',
'wasup',
'gammes.index',
'news',
'food',
'cgu',
'policy',
'search',
'contact',
],
/**
* Model namespaces with filters
*
* Ex: (model_filters & relation_filters are not necessaries)
* 'cruds' => [
* [
* 'model' => 'App\Models\Product',
* 'model_filters' => [
* ['published', '=', true]
* ],
* 'relation_filters' => [
* 'range' => fn(Builder $q) => $q->where('status', true),
* ],
* 'allow_null_relation' => ['range'],
* ],
* [
* 'model' => 'App\Models\Article',
* ]
* ],
*/
'cruds' => [
[
'model' => 'App\Models\Gamme',
],
[
'model' => 'App\Models\Product',
'model_filters' => [
['is_published', '=', true]
],
]
],
/**
* Enable or disable schedule for the job
* If set to true the sitemap generator will be run
* using the 'cron' instruction
*/
'schedule' => true,
/**
* If you want to set specific
* instruction for when to schedule job
* you can set this with a cron instruction
* You can generate one using https://crontab.guru/
*
* schedule must be set to true
*
* Default is every day at 15:45
*/
'cron' => '45 15 * * *',
/**
* All supported locales in the project.
* When used with mcamara/laravel-localization, you can get the values from the package config :
* 'supported_locales' => array_keys(config('laravellocalization.supportedLocales', ['fr' => "Fr"]))
*/
'supported_locales' => ['fr', 'en', 'es'],
/**
* The location where the sitemap file should be generated.
*/
'output' => [
'storage_disk' => 'public',
'path' => 'sitemap.xml',
],
/**
* The route localizer strategy to use.
* Supported: 'default', 'mcamara', or a custom class implementing RouteLocalizerInterface.
*/
'localizer' => 'default',
];

View file

@ -1,75 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.doux.com/</loc>
<changefreq>monthly</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>https://www.doux.com/marque</loc>
</url>
<url>
<loc>https://www.doux.com/gammes</loc>
</url>
<url>
<loc>https://www.doux.com/actualites</loc>
</url>
<url>
<loc>https://www.doux.com/food-service</loc>
</url>
<url>
<loc>https://www.doux.com/mentions-legales</loc>
</url>
<url>
<loc>https://www.doux.com/charte-confidentialite</loc>
</url>
<url>
<loc>https://www.doux.com/contact</loc>
</url>
<!-- ES -->
<url>
<loc>https://www.doux.com/es/marca</loc>
</url>
<url>
<loc>https://www.doux.com/es/productos</loc>
</url>
<url>
<loc>https://www.doux.com/es/noticias</loc>
</url>
<url>
<loc>https://www.doux.com/es/food-service</loc>
</url>
<url>
<loc>https://www.doux.com/es/terminos-y-condiciones-de-uso</loc>
</url>
<url>
<loc>https://www.doux.com/es/política-de-privacidad</loc>
</url>
<url>
<loc>https://www.doux.com/es/contacto</loc>
</url>
<!-- EN -->
<url>
<loc>https://www.doux.com/en/brand</loc>
</url>
<url>
<loc>https://www.doux.comen/en/products</loc>
</url>
<url>
<loc>https://www.doux.com/en/news</loc>
</url>
<url>
<loc>https://www.doux.com/en/food-service</loc>
</url>
<url>
<loc>https://www.doux.com/en/terms-and-conditions</loc>
</url>
<url>
<loc>https://www.doux.com/en/privacy-policy</loc>
</url>
<url>
<loc>https://www.doux.com/en/contact</loc>
</url>
</urlset>

View file

@ -114,60 +114,58 @@ Route::prefix('bo')
}
);
if (!app()->runningInConsole()) {
$langs = ['en', 'es', ''];
$langs = ['en', 'es', ''];
foreach ($langs as $lang) {
Route::middleware(['front-end-globals'])
->prefix($lang)
->name($lang == '' ? '' : $lang . '.')
->group(
function () use ($lang): void {
Route::get('/', [ControllersHomeController::class, 'index'])->name('home');
foreach ($langs as $lang) {
Route::middleware(['front-end-globals'])
->prefix($lang)
->name($lang == '' ? '' : $lang . '.')
->group(
function () use ($lang): void {
Route::get('/', [ControllersHomeController::class, 'index'])->name('home');
Route::get(
'/' . Str::slug(Lang::get('urls.marque', [], $lang)),
[ControllersMarqueController::class, 'index']
)->name('wasup');
Route::get(
'/' . Str::slug(Lang::get('urls.gammes', [], $lang)),
[ControllersGammesController::class, 'index']
)->name('gammes.index');
Route::get(
'/' . Str::slug(Lang::get('urls.gamme', [], $lang)) . '/{gammeSlug:slug}',
[ControllersGammesController::class, 'show']
)->name('gammes.show');
Route::get(
'/' . Str::slug(Lang::get('urls.produit', [], $lang)) . '/{gammeSlug:slug}/{productSlug:slug}',
[ControllersProductsController::class, 'show']
)->name('product.show');
Route::get(
'/' . Str::slug(Lang::get('urls.news', [], $lang)),
[ControllersNewsController::class, 'index']
)->name('news');
Route::get(
'/' . Str::slug(Lang::get('urls.food-service', [], $lang)),
[ControllersFoodServiceController::class, 'index']
)->name('food');
Route::get(
'/' . Str::slug(Lang::get('urls.legals', [], $lang)),
[ControllersLegalsController::class, 'index']
)->name('cgu');
Route::get(
'/' . Str::slug(Lang::get('urls.policy', [], $lang)),
[ControllersPolicyController::class, 'index']
)->name('policy');
Route::get(
'/' . Str::slug(Lang::get('urls.search', [], $lang)),
[ControllersProductsController::class, 'search']
)->name('search');
// Classic.
Route::get(
'/' . Str::slug(Lang::get('urls.contact', [], $lang)),
[ControllersContactController::class, 'contact']
)->name('contact');
Route::post('/contact', [ControllersContactController::class, 'contactSubmit'])->name('contact.submit');
}
);
}//end foreach
}//end if
Route::get(
'/' . Str::slug(Lang::get('urls.marque', [], $lang)),
[ControllersMarqueController::class, 'index']
)->name('wasup');
Route::get(
'/' . Str::slug(Lang::get('urls.gammes', [], $lang)),
[ControllersGammesController::class, 'index']
)->name('gammes.index');
Route::get(
'/' . Str::slug(Lang::get('urls.gamme', [], $lang)) . '/{gammeSlug:slug}',
[ControllersGammesController::class, 'show']
)->name('gammes.show');
Route::get(
'/' . Str::slug(Lang::get('urls.produit', [], $lang)) . '/{gammeSlug:slug}/{productSlug:slug}',
[ControllersProductsController::class, 'show']
)->name('product.show');
Route::get(
'/' . Str::slug(Lang::get('urls.news', [], $lang)),
[ControllersNewsController::class, 'index']
)->name('news');
Route::get(
'/' . Str::slug(Lang::get('urls.food-service', [], $lang)),
[ControllersFoodServiceController::class, 'index']
)->name('food');
Route::get(
'/' . Str::slug(Lang::get('urls.legals', [], $lang)),
[ControllersLegalsController::class, 'index']
)->name('cgu');
Route::get(
'/' . Str::slug(Lang::get('urls.policy', [], $lang)),
[ControllersPolicyController::class, 'index']
)->name('policy');
Route::get(
'/' . Str::slug(Lang::get('urls.search', [], $lang)),
[ControllersProductsController::class, 'search']
)->name('search');
// Classic.
Route::get(
'/' . Str::slug(Lang::get('urls.contact', [], $lang)),
[ControllersContactController::class, 'contact']
)->name('contact');
Route::post('/contact', [ControllersContactController::class, 'contactSubmit'])->name('contact.submit');
}
);
}//end foreach