예제 #1
0
 /**
  * @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);
     }
 }