/** * Tries to match a request with a set of routes. * * If the matcher can not find information, it must throw one of the exceptions documented * below. * * @param Request $request The request to match * * @throws \Exception|\Thelia\Exception\UrlRewritingException * @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException * @return array An array of parameters * */ public function matchRequest(Request $request) { if (ConfigQuery::isRewritingEnable()) { $urlTool = URL::getInstance(); $pathInfo = $request instanceof TheliaRequest ? $request->getRealPathInfo() : $request->getPathInfo(); try { $rewrittenUrlData = $urlTool->resolve($pathInfo); } catch (UrlRewritingException $e) { switch ($e->getCode()) { case UrlRewritingException::URL_NOT_FOUND: throw new ResourceNotFoundException(); break; default: throw $e; } } // If we have a "lang" parameter, whe have to check if the found URL has the proper locale // If it's not the case, find the rewritten URL with the requested locale, and redirect to it. if (null == !($requestedLocale = $request->get('lang'))) { if (null !== ($requestedLang = LangQuery::create()->findOneByLocale($requestedLocale))) { if ($requestedLang->getLocale() != $rewrittenUrlData->locale) { $localizedUrl = $urlTool->retrieve($rewrittenUrlData->view, $rewrittenUrlData->viewId, $requestedLang->getLocale())->toString(); $this->redirect($urlTool->absoluteUrl($localizedUrl), 301); } } } /* is the URL redirected ? */ if (null !== $rewrittenUrlData->redirectedToUrl) { $this->redirect($urlTool->absoluteUrl($rewrittenUrlData->redirectedToUrl), 301); } /* define GET arguments in request */ if (null !== $rewrittenUrlData->view) { $request->attributes->set('_view', $rewrittenUrlData->view); if (null !== $rewrittenUrlData->viewId) { $request->query->set($rewrittenUrlData->view . '_id', $rewrittenUrlData->viewId); } } if (null !== $rewrittenUrlData->locale) { $this->manageLocale($rewrittenUrlData, $request); } foreach ($rewrittenUrlData->otherParameters as $parameter => $value) { $request->query->set($parameter, $value); } return array('_controller' => 'Thelia\\Controller\\Front\\DefaultController::noAction', '_route' => 'rewrite', '_rewritten' => true); } throw new ResourceNotFoundException(); }
/** * Tries to match a request with a set of routes. * * If the matcher can not find information, it must throw one of the exceptions documented * below. * * @param Request $request The request to match * * @throws \Exception|\Thelia\Exception\UrlRewritingException * @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException * @return array An array of parameters * */ public function matchRequest(Request $request) { if (ConfigQuery::isRewritingEnable()) { $urlTool = URL::getInstance(); $pathInfo = $request instanceof TheliaRequest ? $request->getRealPathInfo() : $request->getPathInfo(); try { $rewrittenUrlData = $urlTool->resolve($pathInfo); } catch (UrlRewritingException $e) { switch ($e->getCode()) { case UrlRewritingException::URL_NOT_FOUND: throw new ResourceNotFoundException(); break; default: throw $e; } } /* is the URL redirected ? */ if (null !== $rewrittenUrlData->redirectedToUrl) { $this->redirect($urlTool->absoluteUrl($rewrittenUrlData->redirectedToUrl), 301); } /* define GET arguments in request */ if (null !== $rewrittenUrlData->view) { $request->attributes->set('_view', $rewrittenUrlData->view); if (null !== $rewrittenUrlData->viewId) { $request->query->set($rewrittenUrlData->view . '_id', $rewrittenUrlData->viewId); } } if (null !== $rewrittenUrlData->locale) { $this->manageLocale($rewrittenUrlData, $request); } foreach ($rewrittenUrlData->otherParameters as $parameter => $value) { $request->query->set($parameter, $value); } return array('_controller' => 'Thelia\\Controller\\Front\\DefaultController::noAction', '_route' => 'rewrite', '_rewritten' => true); } throw new ResourceNotFoundException(); }