Skip to content

Commit

Permalink
Correctly generate the index route
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jul 20, 2020
1 parent 351c32f commit ab6cb7e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
16 changes: 16 additions & 0 deletions core-bundle/src/Routing/ContentResolvingGenerator.php
Expand Up @@ -54,6 +54,22 @@ public function generate($name, $parameters = [], $referenceType = self::ABSOLUT
// The route has a cache of its own and is not recompiled as long as it does not get modified
$compiledRoute = $route->compile();

if (
$route instanceof PageRoute
&& 0 === \count(array_intersect_key(array_filter($parameters), array_flip($compiledRoute->getVariables())))
) {
$indexPath = ($route->getUrlPrefix() ? '/'.$route->getUrlPrefix() : '').'/index';

if (
$compiledRoute->getStaticPrefix() === $indexPath
|| $compiledRoute->getStaticPrefix() === $indexPath.$route->getUrlSuffix()
) {
$route->setPath('/');
$route->setUrlSuffix('');
$compiledRoute = $route->compile();
}
}

return $this->doGenerate(
$compiledRoute->getVariables(),
$route->getDefaults(),
Expand Down
@@ -0,0 +1,25 @@
tl_page:
- id: 1
pid: 0
sorting: 128
tstamp: 1539679767
title: Root with home page
alias: root-with-home-page
urlPrefix: 'en'
urlSuffix: '.html'
type: root
language: en
dns: root-with-home.local
fallback: '1'
includeLayout: '1'
layout: 1
published: '1'

- id: 2
pid: 1
sorting: 128
tstamp: 1539698035
title: Home
alias: home
type: regular
published: '1'
Expand Up @@ -5,6 +5,8 @@ tl_page:
tstamp: 1539679767
title: Root with home page
alias: root-with-home-page
urlPrefix: ''
urlSuffix: '.html'
type: root
language: en
dns: root-with-home.local
Expand Down
64 changes: 64 additions & 0 deletions core-bundle/tests/Routing/ContentResolvingGeneratorTest.php
Expand Up @@ -86,4 +86,68 @@ public function testGeneratesTheContentRoute(): void

$this->assertSame('https://www.example.com/some-language/foobar.html', $url);
}

public function testReplacesTheRoutePathForTheIndexRouteWithoutParameters(): void
{
/** @var PageModel&MockObject $page */
$page = $this->mockClassWithProperties(PageModel::class, [
'id' => 17,
'alias' => 'index',
'type' => 'regular',
'domain' => 'www.example.com',
'rootUseSSL' => true,
'urlPrefix' => 'en',
'urlSuffix' => '.html',
]);

$content = (object) ['foo' => 'bar'];
$route = new PageRoute($page);

$this->routeFactory
->expects($this->once())
->method('createRouteForContent')
->with($content)
->willReturn($route)
;

$url = $this->generator->generate(
PageRoute::ROUTE_NAME,
[PageRoute::CONTENT_PARAMETER => $content],
UrlGeneratorInterface::ABSOLUTE_URL
);

$this->assertSame('https://www.example.com/en/', $url);
}

public function testReplacesTheRoutePathForTheIndexRouteWithParameters(): void
{
/** @var PageModel&MockObject $page */
$page = $this->mockClassWithProperties(PageModel::class, [
'id' => 17,
'alias' => 'index',
'type' => 'regular',
'domain' => 'www.example.com',
'rootUseSSL' => true,
'urlPrefix' => 'en',
'urlSuffix' => '.html',
]);

$content = (object) ['foo' => 'bar'];
$route = new PageRoute($page, '{parameters}', ['parameters' => null]);

$this->routeFactory
->expects($this->once())
->method('createRouteForContent')
->with($content)
->willReturn($route)
;

$url = $this->generator->generate(
PageRoute::ROUTE_NAME,
[PageRoute::CONTENT_PARAMETER => $content],
UrlGeneratorInterface::ABSOLUTE_URL
);

$this->assertSame('https://www.example.com/en/', $url);
}
}

0 comments on commit ab6cb7e

Please sign in to comment.