Networking\InitCmsBundle\Helper\PageHelper::matchContentRouteRequest PHP Méthode

matchContentRouteRequest() public méthode

Fills the request object with the content route parameters if found
public matchContentRouteRequest ( Request $request ) : Request
$request Symfony\Component\HttpFoundation\Request
Résultat Symfony\Component\HttpFoundation\Request
    public function matchContentRouteRequest(Request $request)
    {
        /** @var \Symfony\Cmf\Component\Routing\DynamicRouter $dynamicRouter */
        $dynamicRouter = $this->container->get('networking_init_cms.cms_router');
        $requestParams = $dynamicRouter->matchRequest($request);
        if (is_array($requestParams) && !empty($requestParams)) {
            $request->attributes->add($requestParams);
            unset($requestParams['_route']);
            unset($requestParams['_controller']);
            $request->attributes->set('_route_params', $requestParams);
            $configuration = $request->attributes->get('_template');
            $request->attributes->set('_template', $configuration->getTemplate());
            $request->attributes->set('_template_vars', $configuration->getVars());
            $request->attributes->set('_template_streamable', $configuration->isStreamable());
        }
        return $request;
    }

Usage Example

 /**
  * Returns the corresponding route of the given URL for the locale supplied
  * If none is found it returns the original route object
  *
  * @param $oldUrl
  * @param $locale
  * @return array|\Networking\InitCmsBundle\Component\Routing\Route
  * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  */
 public function getTranslationRoute($oldUrl, $locale)
 {
     $cookies = $this->request->cookies ? $this->request->cookies->all() : array();
     $oldRequest = Request::create($oldUrl, 'GET', array(), $cookies);
     if ($this->request->getSession()) {
         $oldRequest->setSession($this->request->getSession());
     }
     try {
         $request = $this->pageHelper->matchContentRouteRequest($oldRequest);
     } catch (ResourceNotFoundException $e) {
         $request = $oldRequest;
     }
     if (!($content = $request->get('_content', false))) {
         try {
             $route = $this->router->matchRequest(Request::create($oldUrl));
         } catch (ResourceNotFoundException $e) {
             if ($route = $this->router->matchRequest(Request::create('/404'))) {
                 return $route;
             }
             throw new NotFoundHttpException(sprintf('Could not find a translation to "%s" for this request"', $locale));
         }
         if (!array_key_exists('_content', $route)) {
             return $route;
         }
         if (!($content = $route['_content'])) {
             return $route;
         }
     }
     if ($content instanceof PageInterface) {
         $translation = $content->getAllTranslations()->get($locale);
         if (is_null($translation)) {
             //@todo does this make sense, or should we throw an exception
             return array('_route' => 'networking_init_cms_home');
         }
         //return a contentRoute object
         $contentRoute = $translation->getContentRoute()->setContent($translation);
         return ContentRouteManager::generateRoute($contentRoute, $contentRoute->getPath(), '');
     }
     if ($content instanceof PageSnapshotInterface) {
         $content = $this->pageHelper->unserializePageSnapshotData($content);
         $translation = $content->getAllTranslations()->get($locale);
         if ($translation && ($snapshotId = $translation->getId())) {
             /** @var $snapshot PageSnapshotInterface */
             $snapshot = $this->om->getRepository($content->getSnapshotClassType())->findOneBy(array('resourceId' => $snapshotId));
             if ($snapshot) {
                 $contentRoute = $snapshot->getRoute();
                 return ContentRouteManager::generateRoute($contentRoute, $contentRoute->getPath(), '');
             }
         }
     }
     if ($this->fallbackRoute) {
         return $this->fallbackRoute;
     }
     if ($route = $this->router->matchRequest(Request::create('/404'))) {
         return $route;
     }
     //no valid translation found
     throw new NotFoundHttpException(sprintf('Could not find a translation to "%s" for content "%s"', $locale, $content->__toString()));
 }