Skip to content

Commit

Permalink
Add redirect routes if page is requested without languagePrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Apr 8, 2020
1 parent 510da35 commit 02eca55
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core-bundle/src/Resources/contao/pages/PageError404.php
Expand Up @@ -85,7 +85,7 @@ protected function prepare()
$objRootPage = $this->getRootPageFromUrl();

// Forward if the language should be but is not set (see #4028)
if (Config::get('addLanguageToUrl'))
if (System::getContainer()->get('contao.framework')->isLegacyRouting() && Config::get('addLanguageToUrl'))
{
// Get the request string without the script name
$strRequest = Environment::get('relativeRequest');
Expand Down
49 changes: 40 additions & 9 deletions core-bundle/src/Routing/RouteProvider.php
Expand Up @@ -87,7 +87,7 @@ public function getRouteCollectionForRequest(Request $request): RouteCollection

$pages = $this->findPages($candidates);

$this->addRoutesForPages($pages, $routes);
$this->addRoutesForPages($pages, $routes, $pathInfo);

return $this->createCollectionForRoutes($routes, $request->getLanguages());
}
Expand Down Expand Up @@ -151,10 +151,10 @@ public function getRoutesByNames($names): array
/**
* @param iterable<PageModel> $pages
*/
private function addRoutesForPages(iterable $pages, array &$routes): void
private function addRoutesForPages(iterable $pages, array &$routes, string $pathInfo = null): void
{
foreach ($pages as $page) {
$this->addRoutesForPage($page, $routes);
$this->addRoutesForPage($page, $routes, $pathInfo);
}
}

Expand All @@ -181,7 +181,7 @@ private function createCollectionForRoutes(array $routes, array $languages): Rou
return $collection;
}

private function addRoutesForPage(PageModel $page, array &$routes): void
private function addRoutesForPage(PageModel $page, array &$routes, string $pathInfo = null): void
{
try {
$page->loadDetails();
Expand All @@ -199,9 +199,10 @@ private function addRoutesForPage(PageModel $page, array &$routes): void
$requirements = ['parameters' => '(/.+)?'];
$path = sprintf('/%s{parameters}%s', $page->alias ?: $page->id, $page->urlSuffix);

if ($this->prependLocale) {
$path = '/{_locale}'.$path;
$requirements['_locale'] = $page->rootLanguage;
if ('' !== $page->languagePrefix) {
$this->addLocaleRedirect($page, $path, $requirements, $pathInfo, $routes);

$path = '/'.$page->languagePrefix.$path;
}

$routes['tl_page.'.$page->id] = new Route(
Expand All @@ -216,6 +217,23 @@ private function addRoutesForPage(PageModel $page, array &$routes): void
$this->addRoutesForRootPage($page, $routes);
}

private function addLocaleRedirect(PageModel $page, string $path, array $requirements, ?string $pathInfo, array &$routes): void
{
$defaults = [
'_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction',
'path' => '/'.$page->languagePrefix.($pathInfo ?: ('/'.$page->alias.$page->urlSuffix)),
'permanent' => true,
];

$routes['tl_page.'.$page->id.'.locale'] = new Route(
$path,
$defaults,
$requirements,
['utf8' => true],
$page->domain
);
}

private function addRoutesForRootPage(PageModel $page, array &$routes): void
{
if ('root' !== $page->type && 'index' !== $page->alias && '/' !== $page->alias) {
Expand Down Expand Up @@ -329,8 +347,13 @@ private function sortRoutes(array &$routes, array $languages = null): void
uasort(
$routes,
static function (Route $a, Route $b) use ($languages, $routes) {
$fallbackA = '.fallback' === substr(array_search($a, $routes, true), -9);
$fallbackB = '.fallback' === substr(array_search($b, $routes, true), -9);
$nameA = array_search($a, $routes, true);
$nameB = array_search($b, $routes, true);

$fallbackA = 0 === substr_compare($nameA, '.fallback', -9);
$fallbackB = 0 === substr_compare($nameB, '.fallback', -9);
$localeA = 0 === substr_compare($nameA, '.locale', -7);
$localeB = 0 === substr_compare($nameB, '.locale', -7);

if ($fallbackA && !$fallbackB) {
return 1;
Expand All @@ -340,6 +363,14 @@ static function (Route $a, Route $b) use ($languages, $routes) {
return -1;
}

if ($localeA && !$localeB) {
return 1;
}

if ($localeB && !$localeA) {
return -1;
}

if ('' !== $a->getHost() && '' === $b->getHost()) {
return -1;
}
Expand Down

0 comments on commit 02eca55

Please sign in to comment.