Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Introduce Page-based URL handling
This feature adds a new database field "pages.slug" which allows to fill the database with URL segments which can then be resolved and built with for URLs for a specific page. On top, when a site is found with a proper "slug", the PageRouter of a site now resolves a /home/my-products/ to the correct page ID. Next steps: - Add URL enhancers API to allow to further resolve more parts. Resolves: #85947 Releases: master Change-Id: Ic64a758e847520b9a8dfc8b484c7613c9ba1f869 Reviewed-on: https://review.typo3.org/57994 Tested-by: Björn Jacob <bjoern.jacob@tritum.de> Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch> Tested-by: Christian Kuhn <lolli@schwarzbu.ch> Reviewed-by: Susanne Moog <susanne.moog@typo3.org> Tested-by: Susanne Moog <susanne.moog@typo3.org> Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl> Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de> Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de> Tested-by: TYPO3com <no-reply@typo3.com> Reviewed-by: Richard Haeser <richard@maxserv.com> Reviewed-by: Oliver Hader <oliver.hader@typo3.org> Tested-by: Oliver Hader <oliver.hader@typo3.org>
- Loading branch information
Showing
14 changed files
with
411 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace TYPO3\CMS\Core\Routing; | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Symfony\Component\Routing\Exception\ResourceNotFoundException; | ||
use Symfony\Component\Routing\Matcher\UrlMatcher; | ||
use Symfony\Component\Routing\RequestContext; | ||
use Symfony\Component\Routing\Route; | ||
use Symfony\Component\Routing\RouteCollection; | ||
use TYPO3\CMS\Core\Database\ConnectionPool; | ||
use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer; | ||
use TYPO3\CMS\Core\Site\Entity\SiteInterface; | ||
use TYPO3\CMS\Core\Site\Entity\SiteLanguage; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
/** | ||
* Page Router looking up the slug of the page path. | ||
* | ||
* This is done via the "Route Candidate" pattern. | ||
* | ||
* Example: | ||
* - /about-us/team/management/ | ||
* | ||
* will look for all pages that have | ||
* - /about-us | ||
* - /about-us/ | ||
* - /about-us/team | ||
* - /about-us/team/ | ||
* - /about-us/team/management | ||
* - /about-us/team/management/ | ||
* | ||
* And create route candidates for that. | ||
* | ||
* PageRouter does not restrict the HTTP method or is bound to any domain constraints, | ||
* as the SiteMatcher has done that already. | ||
* | ||
* @internal This API is not public yet and might change in the future, until TYPO3 v9 or TYPO3 v10. | ||
*/ | ||
class PageRouter | ||
{ | ||
/** | ||
* @param ServerRequestInterface $request | ||
* @param string $routePath | ||
* @param SiteInterface $site | ||
* @param SiteLanguage $language | ||
* @return array|null | ||
*/ | ||
public function matchRoute(ServerRequestInterface $request, string $routePath, SiteInterface $site, SiteLanguage $language): ?array | ||
{ | ||
$slugCandidates = $this->getCandidateSlugsFromRoutePath($routePath); | ||
if (empty($slugCandidates)) { | ||
return null; | ||
} | ||
$pageCandidates = $this->getPagesFromDatabaseForCandidates($slugCandidates, $site, $language->getLanguageId()); | ||
// Stop if there are no candidates | ||
if (empty($pageCandidates)) { | ||
return null; | ||
} | ||
|
||
$collection = new RouteCollection(); | ||
foreach ($pageCandidates ?? [] as $page) { | ||
$path = $page['slug']; | ||
$route = new Route( | ||
$path . '{next}', | ||
['page' => $page, 'next' => ''], | ||
['next' => '.*'], | ||
['utf8' => true] | ||
); | ||
$collection->add('page_' . $page['uid'], $route); | ||
} | ||
|
||
$context = new RequestContext('/', $request->getMethod(), $request->getUri()->getHost()); | ||
$matcher = new UrlMatcher($collection, $context); | ||
try { | ||
return $matcher->match('/' . $routePath); | ||
} catch (ResourceNotFoundException $e) { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Check for records in the database which matches one of the slug candidates. | ||
* | ||
* @param array $slugCandidates | ||
* @param SiteInterface $site | ||
* @param int $languageId | ||
* @return array | ||
*/ | ||
protected function getPagesFromDatabaseForCandidates(array $slugCandidates, SiteInterface $site, int $languageId): array | ||
{ | ||
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) | ||
->getQueryBuilderForTable('pages'); | ||
$queryBuilder | ||
->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class)); | ||
|
||
$statement = $queryBuilder | ||
->select('uid', 'l10n_parent', 'pid', 'slug') | ||
->from('pages') | ||
->where( | ||
$queryBuilder->expr()->eq( | ||
'sys_language_uid', | ||
$queryBuilder->createNamedParameter($languageId, \PDO::PARAM_INT) | ||
), | ||
$queryBuilder->expr()->in( | ||
'slug', | ||
$queryBuilder->createNamedParameter( | ||
$slugCandidates, | ||
Connection::PARAM_STR_ARRAY | ||
) | ||
) | ||
) | ||
// Exact match will be first, that's important | ||
->orderBy('slug', 'desc') | ||
->execute(); | ||
|
||
$pages = []; | ||
$siteMatcher = GeneralUtility::makeInstance(SiteMatcher::class); | ||
while ($row = $statement->fetch()) { | ||
$pageIdInDefaultLanguage = (int)($languageId > 0 ? $row['l10n_parent'] : $row['uid']); | ||
if ($siteMatcher->matchByPageId($pageIdInDefaultLanguage)->getRootPageId() === $site->getRootPageId()) { | ||
$pages[] = $row; | ||
} | ||
} | ||
return $pages; | ||
} | ||
|
||
/** | ||
* Returns possible URL parts for a string like /home/about-us/offices/ | ||
* to return | ||
* /home/about-us/offices/ | ||
* /home/about-us/offices | ||
* /home/about-us/ | ||
* /home/about-us | ||
* /home/ | ||
* /home | ||
* | ||
* @param string $routePath | ||
* @return array | ||
*/ | ||
protected function getCandidateSlugsFromRoutePath(string $routePath): array | ||
{ | ||
$candidatePathParts = []; | ||
$pathParts = GeneralUtility::trimExplode('/', $routePath, true); | ||
while (!empty($pathParts)) { | ||
$prefix = '/' . implode('/', $pathParts); | ||
$candidatePathParts[] = $prefix . '/'; | ||
$candidatePathParts[] = $prefix; | ||
array_pop($pathParts); | ||
} | ||
return $candidatePathParts; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.