Merge branch 'locales-remake' into 'develop'
6.0.0 See merge request ldc-web/projets/sitemap-generator!4
This commit is contained in:
commit
abaa7e5b97
7 changed files with 1469 additions and 1088 deletions
29
CHANGELOG.md
29
CHANGELOG.md
|
|
@ -6,6 +6,35 @@ All notable changes to `sitemap-generator` will be documented in this file.
|
|||
- This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html),
|
||||
- Commits respect [Conventionnal commits](https://www.conventionalcommits.org/en/v1.0.0/) & use [Gitmoji](https://gitmoji.dev/).
|
||||
|
||||
## **[v6.0.0] - 31/03/2026**
|
||||
|
||||
### Added
|
||||
|
||||
- Re-think the locale system to match new Google expectations
|
||||
- Added news sitemap (see https://developers.google.com/search/docs/crawling-indexing/sitemaps/news-sitemap?hl=fr)
|
||||
|
||||
### Migrate to 6.0.0
|
||||
|
||||
- The locales are now in `sitemap-generator.php` config file (previously in `localized-routes.php`). You should add the `supported_locales` line as an array of locales.
|
||||
|
||||
- Your models should now implements `Uwil\SitemapGenerator\Interfaces\Sitemapable` instead of `Spatie\Sitemap\Contracts\Sitemapable`. The new one includes a new method named `getLocalizedUrl()`. This method should retur an URL as a string. Here's an exemple for a News model in a project with [Mcamara\LaravelLocalization](https://github.com/mcamara/laravel-localization) :
|
||||
|
||||
```php
|
||||
public function getLocalizedUrl(string $locale): string
|
||||
{
|
||||
return LaravelLocalization::getURLFromRouteNameTranslated(
|
||||
$locale,
|
||||
'routes.news',
|
||||
['news' => $this]
|
||||
) ?? route('front.news.show', $this);
|
||||
}
|
||||
```
|
||||
|
||||
- For News models, you should set the paramater `isNews` to `true` when calling `SitemapHelper::generateUrl()` in `toSitemapTag`. The model should have a `published_at` and a `title` columns.
|
||||
```text
|
||||
⚠️ Only news with a published_at date within the last 2 days will be added to the sitemap.
|
||||
```
|
||||
|
||||
## **[v5.0.1] - 18/07/2025**
|
||||
|
||||
### Added
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
{
|
||||
"name": "uwil/sitemap-generator",
|
||||
"description": "Create sitemap.xml",
|
||||
"version": "5.0.1",
|
||||
"version": "6.0.0",
|
||||
"require": {
|
||||
"illuminate/support": "~11|~12",
|
||||
"laravel/framework": "~11|~12",
|
||||
"spatie/laravel-sitemap": "^7.0"
|
||||
"spatie/laravel-sitemap": "^8.1",
|
||||
"mcamara/laravel-localization": "^2.4"
|
||||
},
|
||||
"require-dev": {
|
||||
"slevomat/coding-standard": "^8.15"
|
||||
|
|
|
|||
2398
composer.lock
generated
2398
composer.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -52,4 +52,11 @@ return [
|
|||
* 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' => "Fr"]
|
||||
];
|
||||
|
|
|
|||
|
|
@ -40,7 +40,12 @@ class GenerateSitemapCommand extends Command
|
|||
$sitemap = Sitemap::create();
|
||||
|
||||
foreach (config('sitemap-generator.singlePages') as $page) {
|
||||
$sitemap->add(SitemapHelper::generateUrl($page));
|
||||
foreach (config('sitemap-generator.supported_locales') as $locale) {
|
||||
$sitemap->add(SitemapHelper::generateUrl(
|
||||
SitemapHelper::getURLFromRouteNameTranslated($locale, $page),
|
||||
getAlternate: fn ($locale) => SitemapHelper::getURLFromRouteNameTranslated($locale, $page)
|
||||
));
|
||||
}
|
||||
}
|
||||
foreach (config('sitemap-generator.cruds') as $crud) {
|
||||
$modelFilters = $crud['modelFilters'] ?? [];
|
||||
|
|
|
|||
13
src/Interfaces/Sitemapable.php
Normal file
13
src/Interfaces/Sitemapable.php
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Uwil\SitemapGenerator\Interfaces;
|
||||
|
||||
use Spatie\Sitemap\Contracts\Sitemapable as BaseSitemapable;
|
||||
use Spatie\Sitemap\Tags\Url;
|
||||
|
||||
interface Sitemapable extends BaseSitemapable
|
||||
{
|
||||
public function toSitemapTag(): Url | string | array;
|
||||
|
||||
public function getLocalizedUrl(string $locale): string;
|
||||
}
|
||||
|
|
@ -3,6 +3,8 @@
|
|||
namespace Uwil\SitemapGenerator\Lib;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
||||
use Spatie\Sitemap\Tags\Url;
|
||||
|
||||
class SitemapHelper
|
||||
|
|
@ -11,25 +13,97 @@ class SitemapHelper
|
|||
* generateUrl method
|
||||
*
|
||||
* @param string $route
|
||||
* @param array $params
|
||||
* @param mixed $model
|
||||
* @param array $locales
|
||||
* @param callable $getAlternate
|
||||
* @return Url
|
||||
*/
|
||||
public static function generateUrl(string $route, array $params = [], mixed $model = false)
|
||||
{
|
||||
$locales = collect(config('localized-routes.supported_locales'));
|
||||
$url = Url::create(route($route, $params));
|
||||
$url->setLastModificationDate($model ? Carbon::create($model->updated_at) : Carbon::now());
|
||||
public static function generateUrl(
|
||||
string $url,
|
||||
mixed $model = false,
|
||||
array $locales = [],
|
||||
?string $currentLocale = null,
|
||||
?callable $getAlternate = null,
|
||||
bool $isNews = false
|
||||
): Url {
|
||||
$supportedLocales = config('sitemap-generator.supported_locales');
|
||||
$locales = !empty($locales) ?
|
||||
collect(array_intersect($locales, $supportedLocales)) :
|
||||
collect($supportedLocales);
|
||||
|
||||
$sitemapUrl = Url::create($url);
|
||||
$sitemapUrl->setLastModificationDate($model ? Carbon::create($model->updated_at) : Carbon::now());
|
||||
|
||||
if ($locales->count() > 1) {
|
||||
$locales->each(function ($lang) use ($route, $url, $params) {
|
||||
$url->addAlternate(
|
||||
route($route, $params, true, $lang),
|
||||
$lang
|
||||
);
|
||||
$locales->each(function (mixed $lang) use ($sitemapUrl, $url, $model, $getAlternate) {
|
||||
$alternateRoute = $model ?
|
||||
$model->getLocalizedUrl($lang) :
|
||||
self::getAlternateRoute($getAlternate, $lang, $url);
|
||||
|
||||
if ($alternateRoute) {
|
||||
$sitemapUrl->addAlternate(
|
||||
$alternateRoute,
|
||||
$lang
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
return $url;
|
||||
if ($isNews && $model->published_at > Carbon::now()->subDays(2)) {
|
||||
self::addNewsLine($sitemapUrl, $model, $currentLocale);
|
||||
}
|
||||
|
||||
return $sitemapUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add news lines to match these Google expectations :
|
||||
* https://developers.google.com/search/docs/crawling-indexing/sitemaps/news-sitemap?hl=fr
|
||||
* https://spatie.be/docs/laravel-sitemap/v8/adding-content-to-urls/news
|
||||
*
|
||||
* @param Url $sitemapUrl
|
||||
* @param Model $model
|
||||
* @param string $locale
|
||||
* @return void
|
||||
*/
|
||||
private static function addNewsLine(Url $sitemapUrl, Model $model, string $locale)
|
||||
{
|
||||
$sitemapUrl->addNews(
|
||||
$model->getTranslation('title', $locale),
|
||||
$locale,
|
||||
$model->getTranslation('title', $locale),
|
||||
$model->published_at
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL from a given route name, translated with Mcamara\LaravelLocalization package.
|
||||
*
|
||||
* @param string $locale
|
||||
* @param string $routeName
|
||||
* @param string $filename File where the routes translations are. Default is routes.
|
||||
* @return string|false
|
||||
*/
|
||||
public static function getURLFromRouteNameTranslated(
|
||||
string $locale,
|
||||
string $routeName,
|
||||
string $filename = 'routes'
|
||||
): string|false {
|
||||
return LaravelLocalization::getURLFromRouteNameTranslated(
|
||||
$locale,
|
||||
sprintf('%s.%s', $filename, $routeName)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get alternate route from getAlternate if it exists or with laravel route method.
|
||||
*/
|
||||
private static function getAlternateRoute(?callable $getAlternate, string $lang, string $url): string
|
||||
{
|
||||
if ($getAlternate) {
|
||||
return $getAlternate($lang);
|
||||
}
|
||||
|
||||
return route($url, ['locale' => $lang]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue