ldc-groupe/app/Providers/DynamicPageRouteServiceProvider.php

94 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2025-09-16 16:44:49 +02:00
<?php
namespace App\Providers;
2025-10-29 10:29:57 +01:00
use App\Enums\DynamicPages\PageStatusEnum;
use App\Helpers\DynamicPageHelper;
2025-09-16 16:44:49 +02:00
use App\Models\DynamicPage;
use Illuminate\Support\Facades\Route;
2025-09-16 16:44:49 +02:00
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
2025-09-16 16:44:49 +02:00
class DynamicPageRouteServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function boot(): void
{
// Assert that a specific table in the database.
if (!Schema::hasTable('dynamic_pages')) {
2025-09-16 16:44:49 +02:00
return;
}
// Load all published dynamic pages with each parent page relationship.
$dynamicPageModels = DynamicPage::getModelsCached()
->where('status', PageStatusEnum::published)
->load('parentPage');
// Get all locales supported defined in the config.
$supportedLocales = LaravelLocalization::getSupportedLanguagesKeys();
// Get the default locale defined in the project.
$defaultLocale = LaravelLocalization::getDefaultLocale();
// Loop on all locales to generate all routes scoped to their locale.
foreach ($supportedLocales as $locale) {
// Detects whether a prefix should be added or not.
$prefix = $locale === $defaultLocale ? '' : $locale;
Route::group([
// Set the prefix according to the locale.
'prefix' => $prefix,
// Set custom middlewares.
'middleware' => [
'web'
],
], function () use ($locale, $dynamicPageModels) {
foreach ($dynamicPageModels as $dynamicPageModel) {
$slug = $dynamicPageModel->getTranslation('slug', $locale);
// If no slug for the locale → skip.
2026-02-12 17:14:08 +01:00
/** @phpstan-ignore-next-line */
2026-02-10 15:43:47 +01:00
if (!empty($slug) && !$slug) {
continue;
}
// Retrieves the parent segments for this locale.
$segments = DynamicPageHelper::getAllDynamicPageSegments(
$dynamicPageModel,
$dynamicPageModels->keyBy('id'),
$locale
);
// If a parent does not have a slug in this locale → skip.
2026-02-10 15:43:47 +01:00
if ($segments === null) {
continue;
}
$segmentsPath = implode('/', $segments);
$finalPath = $segmentsPath ? "/{$segmentsPath}/{$slug}" : "/{$slug}";
Route::get($finalPath, function () use ($dynamicPageModel, $locale) {
app()->setLocale($locale);
LaravelLocalization::setLocale($locale);
return view($dynamicPageModel->getView(), compact('dynamicPageModel'));
})
->defaults('dynamicPage', $dynamicPageModel)
->name("front.dynamic-pages.{$dynamicPageModel->id_key}.{$locale}");
2026-02-10 15:43:47 +01:00
} //end foreach
});
2026-02-10 15:43:47 +01:00
} //end foreach
// Share all published dynamic pages to all blade views.
foreach ($dynamicPageModels as $dynamicPageModel) {
$pascalCase = str_replace(' ', '', ucwords(str_replace('-', ' ', $dynamicPageModel->id_key)));
View::share(sprintf("page%s", $pascalCase), $dynamicPageModel);
}
2025-09-16 16:44:49 +02:00
}
}