feat: added sitemap.xml with sitemap-generator:6.0.0 + added robots.txt
This commit is contained in:
parent
07597ed8ac
commit
afb716256e
9 changed files with 1009 additions and 421 deletions
|
|
@ -19,6 +19,9 @@ use Illuminate\Validation\ValidationException;
|
|||
use Laravel\Scout\Searchable;
|
||||
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
||||
use Uwil\LaravelActivityLogs\Traits\ActivityLog;
|
||||
use Spatie\Sitemap\Contracts\Sitemapable;
|
||||
use Spatie\Sitemap\Tags\Url;
|
||||
use Uwil\SitemapGenerator\Lib\SitemapHelper;
|
||||
|
||||
/**
|
||||
* @property string $id_key Unique key identification of the dynamic page.
|
||||
|
|
@ -34,7 +37,7 @@ use Uwil\LaravelActivityLogs\Traits\ActivityLog;
|
|||
* @property-read \Illuminate\Support\Carbon $updated_at Date on which the dynamic page was updated.
|
||||
* @property-read \Illuminate\Support\Carbon $deleted_at Date on which the dynamic page was updated.
|
||||
*/
|
||||
class DynamicPage extends Model implements TranslatableModel
|
||||
class DynamicPage extends Model implements TranslatableModel, Sitemapable
|
||||
{
|
||||
use ActivityLog;
|
||||
use HasCache;
|
||||
|
|
@ -472,6 +475,47 @@ class DynamicPage extends Model implements TranslatableModel
|
|||
return $data;
|
||||
}
|
||||
|
||||
// * SITEMAP
|
||||
|
||||
/**
|
||||
* toSitemapTag method
|
||||
*
|
||||
* @return Url|string|array
|
||||
*/
|
||||
public function toSitemapTag(): Url | string | array
|
||||
{
|
||||
$sitemapLines = [];
|
||||
$locales = array_keys(config('laravellocalization.supportedLocales', ['fr' => "Français"]));
|
||||
|
||||
foreach ($locales as $locale) {
|
||||
$slug = $this->getTranslation('slug', $locale, false);
|
||||
|
||||
// Homepage has no slug.
|
||||
if ($slug || $this->isRoot) {
|
||||
$sitemapLines[] = SitemapHelper::generateUrl(
|
||||
$this->getLocalizedUrl($locale),
|
||||
locales: $this->locales(),
|
||||
getAlternate: fn (string $locale) => $this->getLocalizedUrl($locale)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $sitemapLines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get localized URL.
|
||||
*
|
||||
* @param string $locale
|
||||
* @return string|false
|
||||
*/
|
||||
public function getLocalizedUrl(string $locale): string|false
|
||||
{
|
||||
return in_array($locale, $this->locales()) ?
|
||||
route("front.dynamic-pages.$this->id_key.$locale") :
|
||||
false;
|
||||
}
|
||||
|
||||
// * RELATIONSHIPS
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -17,8 +17,12 @@ use Illuminate\Support\Arr;
|
|||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Scout\Searchable;
|
||||
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
||||
use Mcamara\LaravelLocalization\Interfaces\LocalizedUrlRoutable;
|
||||
use Uwil\LaravelActivityLogs\Traits\ActivityLog;
|
||||
use Spatie\Sitemap\Tags\Url;
|
||||
use Uwil\SitemapGenerator\Interfaces\Sitemapable;
|
||||
use Uwil\SitemapGenerator\Lib\SitemapHelper;
|
||||
|
||||
/**
|
||||
* @property string $seo_title
|
||||
|
|
@ -56,7 +60,7 @@ use Uwil\LaravelActivityLogs\Traits\ActivityLog;
|
|||
* @property \Illuminate\Database\Eloquent\Collection<int, \App\Models\NewsTag> $tags
|
||||
* Associated news tags of the news.
|
||||
*/
|
||||
class News extends Model implements TranslatableModel, LocalizedUrlRoutable
|
||||
class News extends Model implements TranslatableModel, LocalizedUrlRoutable, Sitemapable
|
||||
{
|
||||
use ActivityLog;
|
||||
use HasCache;
|
||||
|
|
@ -466,6 +470,49 @@ class News extends Model implements TranslatableModel, LocalizedUrlRoutable
|
|||
});
|
||||
}
|
||||
|
||||
// * SITEMAP
|
||||
|
||||
/**
|
||||
* toSitemapTag method
|
||||
*
|
||||
* @return Url|string|array
|
||||
*/
|
||||
public function toSitemapTag(): Url | string | array
|
||||
{
|
||||
$model = $this;
|
||||
$locales = $model->locales();
|
||||
$sitemapUrls = [];
|
||||
|
||||
foreach ($locales as $locale) {
|
||||
if (in_array($locale, config('sitemap-generator.supported_locales'))) {
|
||||
$sitemapUrls[] = SitemapHelper::generateUrl(
|
||||
$model->getLocalizedUrl($locale),
|
||||
$model,
|
||||
$locales,
|
||||
currentLocale: $locale,
|
||||
isNews: true
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $sitemapUrls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get localized URL from Mcamara\LaravelLocalization.
|
||||
*
|
||||
* @param string $locale
|
||||
* @return string
|
||||
*/
|
||||
public function getLocalizedUrl(string $locale): string
|
||||
{
|
||||
return LaravelLocalization::getURLFromRouteNameTranslated(
|
||||
$locale,
|
||||
'routes.front.news',
|
||||
['news' => $this]
|
||||
) ?? route('front.news.show', $this);
|
||||
}
|
||||
|
||||
// * RELATIONSHIPS
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -27,7 +27,9 @@
|
|||
"uwil/admin-secure-laravel": "^5.0",
|
||||
"uwil/k8s-laravel": "^2.0",
|
||||
"uwil/laravel-backend": "^3.0",
|
||||
"uwil/laravel-common": "^4.1"
|
||||
"uwil/laravel-common": "^4.1",
|
||||
"uwil/sitemap-generator": "^6.0",
|
||||
"verschuur/laravel-robotstxt": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"fakerphp/faker": "^1.23",
|
||||
|
|
|
|||
1222
composer.lock
generated
1222
composer.lock
generated
File diff suppressed because it is too large
Load diff
19
config/robots-txt.php
Normal file
19
config/robots-txt.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
return [
|
||||
'environments' => [
|
||||
'production' => [
|
||||
'paths' => [
|
||||
'*' => [
|
||||
'disallow' => [
|
||||
'/admin',
|
||||
'*.pdf$'
|
||||
],
|
||||
'allow' => []
|
||||
],
|
||||
],
|
||||
'sitemaps' => [
|
||||
'sitemap.xml'
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
74
config/sitemap-generator.php
Normal file
74
config/sitemap-generator.php
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
||||
|
||||
return [
|
||||
|
||||
/**
|
||||
* Single route names
|
||||
*
|
||||
* Ex:
|
||||
*
|
||||
* 'singlePages' => [
|
||||
* 'homepage',
|
||||
* ],
|
||||
*/
|
||||
'singlePages' => [
|
||||
'front.search',
|
||||
],
|
||||
|
||||
/**
|
||||
* Model namespaces with filters
|
||||
*
|
||||
* Ex: (modelFilters & relationFilters are not necessaries)
|
||||
* 'cruds' => [
|
||||
* [
|
||||
* 'model' => 'App\Models\Product',
|
||||
* 'modelFilters' => [
|
||||
* ['published', '=', true]
|
||||
* ],
|
||||
* 'relationFilters' => [
|
||||
* 'range' => fn(Builder $q) => $q->where('status', true),
|
||||
* ],
|
||||
* ],
|
||||
* [
|
||||
* 'model' => 'App\Models\Article',
|
||||
* ]
|
||||
* ],
|
||||
*/
|
||||
'cruds' => [
|
||||
[
|
||||
'model' => 'App\Models\News',
|
||||
'filters' => [
|
||||
['status', '=', \App\Enums\News\NewsStatusEnum::published]
|
||||
],
|
||||
],
|
||||
[
|
||||
'model' => 'App\Models\DynamicPage',
|
||||
'filters' => [
|
||||
['status', '=', \App\Enums\DynamicPages\PageStatusEnum::published]
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
/**
|
||||
* 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 * * *',
|
||||
|
||||
'supported_locales' => array_keys(config('laravellocalization.supportedLocales', ['fr' => "Fr"]))
|
||||
];
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
'news' => 'news/{news}',
|
||||
'search' => 'search',
|
||||
'front' => [
|
||||
'news' => 'news/{news}',
|
||||
'search' => 'search'
|
||||
],
|
||||
];
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
'news' => 'actualites/{news}',
|
||||
'search' => 'recherche',
|
||||
'front' => [
|
||||
'news' => 'actualites/{news}',
|
||||
'search' => 'recherche'
|
||||
],
|
||||
];
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ Route::group(
|
|||
|
||||
|
||||
// Search routes.
|
||||
Route::get(LaravelLocalization::transRoute('routes.search'), [SearchController::class, 'search'])
|
||||
Route::get(LaravelLocalization::transRoute('routes.front.search'), [SearchController::class, 'search'])
|
||||
->name('search');
|
||||
Route::post('/search/load/results', [SearchController::class, 'ajaxSearchLoadResults'])
|
||||
->name('search.ajax.news');
|
||||
|
|
@ -40,6 +40,6 @@ Route::group(
|
|||
});
|
||||
|
||||
// News routes.
|
||||
Route::get(LaravelLocalization::transRoute('routes.news'), [NewsController::class, 'show'])->name('news.show');
|
||||
Route::get(LaravelLocalization::transRoute('routes.front.news'), [NewsController::class, 'show'])->name('news.show');
|
||||
}
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue