Skip to content

Commit

Permalink
Merge pull request #79 from softspring/locale-paths-supports-trailing…
Browse files Browse the repository at this point in the history
…-slash-redirects

Locale paths supports trailing slash redirects
  • Loading branch information
javihgil committed May 16, 2023
2 parents ad7592f + a2b8bf7 commit 5503c96
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/Config/Model/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->children()
->scalarNode('path')->isRequired()->end()
->scalarNode('locale')->defaultFalse()->end()
->scalarNode('trailing_slash_on_root')->defaultFalse()->end()
->end()
->end()
->end()
Expand Down
37 changes: 24 additions & 13 deletions src/Routing/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function matchRequest(Request $request): ?array
}

$pathInfo = $request->getPathInfo();
$pathInfoHasTrailingSlash = str_ends_with($pathInfo, '/');

if ($siteConfig['slash_route']['enabled'] && '/' === $pathInfo) {
switch ($siteConfig['slash_route']['behaviour']) {
Expand Down Expand Up @@ -90,7 +91,15 @@ public function matchRequest(Request $request): ?array
}
$attributes['_sfs_cms_locale'] = $path['locale'];
$attributes['_sfs_cms_locale_path'] = $path['path'];

$pathInfo = substr($pathInfo, strlen($path['path']));

if ($path['trailing_slash_on_root'] && '' === $pathInfo && !$pathInfoHasTrailingSlash) {
$url = parse_url($request->getUri());
$url = sprintf('%s://%s%s', $url['scheme'], $url['host'], $url['path'].'/');

return $this->generateRedirect($url, Response::HTTP_MOVED_PERMANENTLY);
}
}
}
}
Expand All @@ -114,22 +123,24 @@ public function matchRequest(Request $request): ?array
return $this->generateRedirect($this->urlGenerator->getUrl($route->getId(), $routePath->getLocale()), $siteConfig['slash_route']['redirect_code'] ?: Response::HTTP_FOUND);
}

switch ($route->getType()) {
case RouteInterface::TYPE_CONTENT:
$attributes['_route'] = $routePath->getRoute()->getId();
$attributes['_route_params'] = [];
$attributes['_controller'] = 'Softspring\CmsBundle\Controller\ContentController::renderRoutePath';
$attributes['routePath'] = $routePath;
break;
if ($attributes['_sfs_cms_locale_path']) {
switch ($route->getType()) {
case RouteInterface::TYPE_CONTENT:
$attributes['_route'] = $routePath->getRoute()->getId();
$attributes['_route_params'] = [];
$attributes['_controller'] = 'Softspring\CmsBundle\Controller\ContentController::renderRoutePath';
$attributes['routePath'] = $routePath;
break;

case RouteInterface::TYPE_REDIRECT_TO_URL:
return $this->generateRedirect($route->getRedirectUrl(), $route->getRedirectType() ?? Response::HTTP_FOUND);
case RouteInterface::TYPE_REDIRECT_TO_URL:
return $this->generateRedirect($route->getRedirectUrl(), $route->getRedirectType() ?? Response::HTTP_FOUND);

case RouteInterface::TYPE_REDIRECT_TO_ROUTE:
return $this->generateRedirectToRoute($route->getSymfonyRoute(), $route->getRedirectType() ?? Response::HTTP_FOUND);
case RouteInterface::TYPE_REDIRECT_TO_ROUTE:
return $this->generateRedirectToRoute($route->getSymfonyRoute(), $route->getRedirectType() ?? Response::HTTP_FOUND);

default:
throw new \Exception(sprintf('Route type %u not yet implemented', $route->getType()));
default:
throw new \Exception(sprintf('Route type %u not yet implemented', $route->getType()));
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Router/UrlMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ public function testSiteWithPathsButRouteNotFound()
'enabled' => false,
],
'paths' => [
['path' => '/es', 'locale' => 'es'],
['path' => '/en', 'locale' => 'en'],
['path' => '/es', 'locale' => 'es', 'trailing_slash_on_root' => false],
['path' => '/en', 'locale' => 'en', 'trailing_slash_on_root' => false],
],
'sitemaps' => [],
'sitemaps_index' => ['enabled' => false],
Expand Down Expand Up @@ -191,8 +191,8 @@ public function testFoundRouteContent()
'enabled' => false,
],
'paths' => [
['path' => '/es', 'locale' => 'es'],
['path' => '/en', 'locale' => 'en'],
['path' => '/es', 'locale' => 'es', 'trailing_slash_on_root' => false],
['path' => '/en', 'locale' => 'en', 'trailing_slash_on_root' => false],
],
'sitemaps' => [],
'sitemaps_index' => ['enabled' => false],
Expand Down

0 comments on commit 5503c96

Please sign in to comment.