public function matchRequest(Request $request)
{
try {
$requestedPath = $request->attributes->get('semanticPathinfo', $request->getPathInfo());
$urlAlias = $this->getUrlAlias($requestedPath);
if ($this->rootLocationId === null) {
$pathPrefix = '/';
} else {
$pathPrefix = $this->generator->getPathPrefixByRootLocationId($this->rootLocationId);
}
$params = array('_route' => self::URL_ALIAS_ROUTE_NAME);
switch ($urlAlias->type) {
case URLAlias::LOCATION:
$location = $this->generator->loadLocation($urlAlias->destination);
$params += array('_controller' => static::VIEW_ACTION, 'contentId' => $location->contentId, 'locationId' => $urlAlias->destination, 'viewType' => ViewManager::VIEW_TYPE_FULL, 'layout' => true);
$request->attributes->set('locationId', $urlAlias->destination);
// For Location alias setup 301 redirect to Location's current URL when:
// 1. alias is history
// 2. alias is custom with forward flag true
// 3. requested URL is not case-sensitive equal with the one loaded
if ($urlAlias->isHistory === true || $urlAlias->isCustom === true && $urlAlias->forward === true) {
$request->attributes->set('semanticPathinfo', $this->generate($location));
$request->attributes->set('needsRedirect', true);
// Specify not to prepend siteaccess while redirecting when applicable since it would be already present (see UrlAliasGenerator::doGenerate())
$request->attributes->set('prependSiteaccessOnRedirect', false);
} elseif ($this->needsCaseRedirect($urlAlias, $requestedPath, $pathPrefix)) {
$request->attributes->set('semanticPathinfo', $this->removePathPrefix($urlAlias->path, $pathPrefix));
$request->attributes->set('needsRedirect', true);
}
if (isset($this->logger)) {
$this->logger->info("UrlAlias matched location #{$urlAlias->destination}. Forwarding to ViewController");
}
break;
case URLAlias::RESOURCE:
// In URLAlias terms, "forward" means "redirect".
if ($urlAlias->forward) {
$request->attributes->set('semanticPathinfo', '/' . trim($urlAlias->destination, '/'));
$request->attributes->set('needsRedirect', true);
} elseif ($this->needsCaseRedirect($urlAlias, $requestedPath, $pathPrefix)) {
// Handle case-correction redirect
$request->attributes->set('semanticPathinfo', $this->removePathPrefix($urlAlias->path, $pathPrefix));
$request->attributes->set('needsRedirect', true);
} else {
$request->attributes->set('semanticPathinfo', '/' . trim($urlAlias->destination, '/'));
$request->attributes->set('needsForward', true);
}
break;
case URLAlias::VIRTUAL:
// Handle case-correction redirect
if ($this->needsCaseRedirect($urlAlias, $requestedPath, $pathPrefix)) {
$request->attributes->set('semanticPathinfo', $this->removePathPrefix($urlAlias->path, $pathPrefix));
$request->attributes->set('needsRedirect', true);
} else {
// Virtual aliases should load the Content at homepage URL
$request->attributes->set('semanticPathinfo', '/');
$request->attributes->set('needsForward', true);
}
break;
}
return $params;
} catch (NotFoundException $e) {
throw new ResourceNotFoundException($e->getMessage(), $e->getCode(), $e);
}
}