Skip to content

Commit

Permalink
Correctly match a request with url prefix if there is no index page
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jul 20, 2020
1 parent ab6cb7e commit b01b059
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
17 changes: 16 additions & 1 deletion core-bundle/src/Routing/Candidates/PageCandidates.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,22 @@ public function getCandidates(Request $request): array
{
$this->initialize();

return parent::getCandidates($request);
$candidates = parent::getCandidates($request);

if (\in_array('index', $candidates, true)) {
$result = $this->connection->executeQuery(
"SELECT IF(alias='', id, alias) FROM tl_page WHERE type='root' AND (dns=:httpHost OR dns='')",
['httpHost' => $request->getHttpHost()]
);

$candidates = array_merge(
$candidates,
['/'],
$result->fetchAll(FetchMode::COLUMN)
);
}

return array_unique($candidates);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion core-bundle/src/Routing/RouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ static function (Route $a, Route $b) use ($languages, $routes) {
private function findRootPages(string $httpHost): array
{
if (
!empty($GLOBALS['TL_HOOKS']['getRootPageFromUrl'])
$this->legacyRouting
&& !empty($GLOBALS['TL_HOOKS']['getRootPageFromUrl'])
&& \is_array($GLOBALS['TL_HOOKS']['getRootPageFromUrl'])
) {
/** @var System $system */
Expand Down
10 changes: 10 additions & 0 deletions core-bundle/tests/Functional/RoutingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,16 @@ public function getAliasesWithLocale(): \Generator
'root-with-home.local',
true,
];

yield 'Redirects to the first regular page if the alias is not "index" and the request is only the prefix' => [
['theme', 'root-with-home-and-prefix'],
'/en/',
302,
'Redirecting to http://root-with-home.local/en/home.html',
['language' => 'en'],
'root-with-home.local',
false,
];
}

/**
Expand Down
20 changes: 16 additions & 4 deletions core-bundle/tests/Routing/Candidates/CandidatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public function getCandidatesProvider(): \Generator
['.html'],
['en'],
[
'default' => ['index'],
'default' => ['index', '/', 'foobar'],
'legacy' => [],
'locale' => [],
],
Expand Down Expand Up @@ -408,20 +408,32 @@ public function getCandidatesProvider(): \Generator
*/
private function mockConnectionWithLanguages(array $languages): Connection
{
$statement = $this->createMock(Statement::class);
$statement
$prefixStatement = $this->createMock(Statement::class);
$prefixStatement
->expects($this->once())
->method('fetchAll')
->with(FetchMode::COLUMN)
->willReturn($languages)
;

$connection = $this->createMock(Connection::class);

$connection
->expects($this->once())
->method('query')
->with("SELECT DISTINCT urlPrefix FROM tl_page WHERE type='root'")
->willReturn($statement)
->willReturn($prefixStatement)
;

$rootStatement = $this->createMock(Statement::class);
$rootStatement
->method('fetchAll')
->willReturn(['foobar'])
;

$connection
->method('executeQuery')
->willReturn($rootStatement)
;

return $connection;
Expand Down

0 comments on commit b01b059

Please sign in to comment.