eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter::matchRequest PHP Method

matchRequest() public method

If the matcher can not find information, it must throw one of the exceptions documented below.
public matchRequest ( Request $request ) : array
$request Symfony\Component\HttpFoundation\Request The request to match
return array An array of parameters
    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);
        }
    }

Usage Example

    public function testMatchRequestVirtualWithCaseRedirect()
    {
        $pathInfo = '/Foo/bAR';
        $urlAliasPath = '/foo/bar';
        $urlAlias = new URLAlias(
            array(
                'path' => $urlAliasPath,
                'type' => UrlAlias::VIRTUAL,
            )
        );
        $request = $this->getRequestByPathInfo( $pathInfo );
        $this->urlALiasGenerator
            ->expects( $this->once() )
            ->method( 'isUriPrefixExcluded' )
            ->with( $pathInfo )
            ->will( $this->returnValue( false ) );
        $this->urlAliasService
            ->expects( $this->once() )
            ->method( 'lookup' )
            ->with( $pathInfo )
            ->will( $this->returnValue( $urlAlias ) );

        $expected = array(
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
        );
        $this->assertEquals( $expected, $this->router->matchRequest( $request ) );
        $this->assertTrue( $request->attributes->get( 'needsRedirect' ) );
        $this->assertSame( $urlAliasPath, $request->attributes->get( 'semanticPathinfo' ) );
    }
All Usage Examples Of eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter::matchRequest