93 lines
3.5 KiB
PHP
93 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Enums\DynamicPages\PageStatusEnum;
|
|
use App\Helpers\DynamicPageHelper;
|
|
use App\Models\DynamicPage;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Support\Facades\View;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
|
|
|
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')) {
|
|
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.
|
|
/** @phpstan-ignore-next-line */
|
|
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.
|
|
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}");
|
|
} //end foreach
|
|
});
|
|
} //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);
|
|
}
|
|
}
|
|
}
|