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); } } }