/** * @param HttpRequest $httpRequest * * @return Response */ public function handle(HttpRequest $httpRequest) { try { $routeParameters = $this->urlMatcher->matchRequest($httpRequest); $httpRequest->attributes->add($routeParameters); $request = $this->extractRequestFromRouteParameters($routeParameters); $controller = $this->extractControllerFromRouteParameters($routeParameters); return $controller->handleRequest($request, $this->serviceContainer); } catch (ValidationExceptionSet $exception) { return $this->errorResponseFactory->createBadRequestResponse($exception); } catch (BadRequestHttpException $exception) { return $this->errorResponseFactory->createBadRequestResponse($exception); } catch (NotFoundHttpException $exception) { return $this->errorResponseFactory->createNotFoundResponse($exception); } catch (MethodNotAllowedException $exception) { $exception = new MethodNotAllowedHttpException($exception->getAllowedMethods(), 'Used request method not allowed. Allowed: ' . implode(', ', $exception->getAllowedMethods())); return $this->errorResponseFactory->createMethodNotAllowedResponse($exception); } catch (\Exception $exception) { return $this->errorResponseFactory->createInternalServerErrorResponse($exception); } }
/** * @param Request $request */ public function match(Request $request) { // Initialize the context that is also used by the generator (assuming matcher and generator share the same // context instance). $this->context->fromRequest($request); if ($request->attributes->has('_controller')) { // Routing is already done. return; } // Add attributes based on the request (routing). try { // Matching a request is more powerful than matching a URL path + context, so try that first. if ($this->matcher instanceof RequestMatcherInterface) { $parameters = $this->matcher->matchRequest($request); } else { $parameters = $this->matcher->match($request->getPathInfo()); } if (null !== $this->logger) { $this->logger->info(sprintf('Matched route "%s" (parameters: %s)', $parameters['_route'], $this->parametersToString($parameters))); } $request->attributes->add($parameters); unset($parameters['_route']); unset($parameters['_controller']); $request->attributes->set('_route_params', $parameters); } catch (ResourceNotFoundException $e) { $message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getPathInfo()); throw new NotFoundHttpException($message, $e); } catch (MethodNotAllowedException $e) { $message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request->getMethod(), $request->getPathInfo(), strtoupper(implode(', ', $e->getAllowedMethods()))); throw new MethodNotAllowedException($e->getAllowedMethods(), $message); } }
protected function getAllowedMethods($uri) { try { $route = $this->requestMatcher->matchRequest(Request::create($uri, 'OPTIONS')); if (isset($route['allowedMethods'])) { return explode(',', $route['allowedMethods']); } } catch (ResourceNotFoundException $e) { // the provider doesn't care about a not found } catch (MethodNotAllowedException $e) { // neither does it care about a method not allowed } return array(); }
/** * Matches a path in the router. * * @param string $path * The request path. * @param array $exclude * An array of paths or system paths to skip. * * @return \Symfony\Component\HttpFoundation\Request * A populated request object or NULL if the path couldn't be matched. */ protected function getRequestForPath($path, array $exclude) { if (!empty($exclude[$path])) { return NULL; } // @todo Use the RequestHelper once https://drupal.org/node/2090293 is // fixed. $request = Request::create($this->context->getBaseUrl() . '/' . $path); // Performance optimization: set a short accept header to reduce overhead in // AcceptHeaderMatcher when matching the request. $request->headers->set('Accept', 'text/html'); // Find the system path by resolving aliases, language prefix, etc. $processed = $this->pathProcessor->processInbound($path, $request); if (empty($processed) || !empty($exclude[$processed])) { // This resolves to the front page, which we already add. return NULL; } $request->attributes->set('_system_path', $processed); // Attempt to match this path to provide a fully built request. try { $request->attributes->add($this->router->matchRequest($request)); return $request; } catch (ParamNotConvertedException $e) { return NULL; } catch (ResourceNotFoundException $e) { return NULL; } catch (MethodNotAllowedException $e) { return NULL; } catch (AccessDeniedHttpException $e) { return NULL; } }
/** * {@inheritdoc} */ public function isValid($path) { // External URLs and the front page are always valid. if ($path == '<front>' || UrlHelper::isExternal($path)) { return TRUE; } // Check the routing system. $collection = $this->routeProvider->getRoutesByPattern('/' . $path); if ($collection->count() == 0) { return FALSE; } $request = RequestHelper::duplicate($this->requestStack->getCurrentRequest(), '/' . $path); $request->attributes->set('_system_path', $path); // We indicate that a menu administrator is running the menu access check. $request->attributes->set('_menu_admin', TRUE); // Attempt to match this path to provide a fully built request to the // access checker. try { $request->attributes->add($this->requestMatcher->matchRequest($request)); } catch (ParamNotConvertedException $e) { return FALSE; } // Consult the access manager. $routes = $collection->all(); $route = reset($routes); return $this->accessManager->check($route, $request, $this->account); }
/** * Matches a path in the router. * * @param \Symfony\Component\HttpFoundation\Request $request * Page request object. * @param string $path * Path to look up. * * @return \Symfony\Component\HttpFoundation\Request|null * A populated request object or NULL if the patch could not be matched. */ protected function getRequestForPath(Request $request, $path) { // @todo Use RequestHelper::duplicate once https://drupal.org/node/2090293 // is fixed. $route_request = Request::create($request->getBaseUrl() . '/' . $path); // Find the system path by resolving aliases, language prefix, etc. $processed = $this->pathProcessor->processInbound($path, $route_request); $route_request->attributes->set('_system_path', $processed); // Attempt to match this path to provide a fully built request. try { $route_request->attributes->add($this->router->matchRequest($route_request)); return $route_request; } catch (ParamNotConvertedException $e) { return NULL; } catch (ResourceNotFoundException $e) { return NULL; } }