public function match($urlOrRequest = null) { $request = $this->makeRequest($urlOrRequest); $matcher = new UrlMatcher($this->getRoutes(), $this->context); $params = $matcher->matchRequest($request); return $this->handler->handle($params, $request); }
/** * Returns the Controller instance associated with a Request. * * As several resolvers can exist for a single application, a resolver must * return false when it is not able to determine the controller. * * The resolver must only throw an exception when it should be able to load * controller but cannot because of some errors made by the developer. * * @param Request $request A Request instance * * @return callable|false A PHP callable representing the Controller, * or false if this resolver is not able to determine the controller * * @throws \LogicException If the controller can't be found * * @api */ public function getController(Request $request) { $route = $this->urlMatcher->matchRequest($request); $controllerClass = sprintf("\\DG\\SymfonyCert\\Controller\\%sController", ucfirst($route['_controller'])); $controller = new $controllerClass($this->container); $attributes = []; foreach ($route as $key => $value) { if (strpos($key, '_') !== 0) { $attributes[$key] = $value; } else { $request->attributes->set($key, $value); } if ($key === '_locale') { $request->getSession()->set('locale', $value); } } $request->attributes->set('data', $attributes); return [$controller, $route['_action'] . 'Action']; }
/** * @return RouteParametersItem */ public function work() { RouteCache::work(self::$routesFile); $this->addRoutesToRouteCollection(); $context = new RequestContext(); $context->fromRequest(Request::request()); $matcher = new UrlMatcher($this->routeCollection, $context); $parameters = $matcher->matchRequest(Request::request()); return new RouteParametersItem($parameters); }
public final function matchRoute(\bolt\browser\route $route, \bolt\browser\request $req) { $collection = b::browser('route\\collection\\create', [$route]); $match = new UrlMatcher($collection, $req->getContext()); // we're going to try and match our request // if not we fall back to error try { return $match->matchRequest($req); } catch (ResourceNotFoundException $e) { return false; } }
/** * @param Request $request * @return Action|null */ public function findAction(Request $request) { $context = new RequestContext(); $context->fromRequest($request->getInternalRequest()); $matcher = new UrlMatcher($this->_routes, $context); try { $params = $matcher->matchRequest($request->getInternalRequest()); } catch (\RuntimeException $exception) { return null; } $controllerName = $params['_controllerName']; $methodName = $params['_methodName']; unset($params['_controllerName']); unset($params['_methodName']); unset($params['_route']); return new Action($controllerName, $methodName, $params); }
function it_should_capture_any_kind_of_error_as_internal_server_error(UrlMatcher $matcher, AnnotExecutor $executor, Request $request, Response $response) { $matcher->matchRequest(Argument::Any())->willThrow("Exception"); $response->setStatusCode(500)->shouldBeCalled(); $this->run($request, $response)->shouldBe($response); }
/** * Resolves a route and its parameters based on the path in the request. * @return array An array of parameters. */ public function resolve() { $currentRoute = $this->_request->server->get('REQUEST_URI'); $context = new RequestContext(); $context->fromRequest($this->_request); $matcher = new UrlMatcher($this->_routeCollection, $context); try { $parameters = $matcher->matchRequest($this->_request); } catch (Exception $e) { if ($this->_defaultController) { return ['controller' => $this->_defaultController, '_route' => 'default']; } throw new Exception('Could not find route "' . $currentRoute . '"'); } return $parameters; }
* a method in our controller (sometimes called an action). This might seem * unnecessary, but routers can be really helpful as your site grows. * * Check out more information here: * * http://symfony.com/doc/current/components/routing/introduction.html * * We're going to start off with two routes for our two different types of page. */ $routes = array('index' => new Route('/', array('_controller' => $siteController, '_method' => 'indexPage')), 'general' => new Route('/hello/{name}', array('_controller' => $siteController, '_method' => 'generalPage'))); // Add our routes to a collection $routeCollection = new RouteCollection(); foreach ($routes as $routeName => $route) { $routeCollection->add($routeName, $route); } /* * Match the request we generated earlier to a route. UrlMatcher::matchRequest * will throw an exception if it cannot match a request. * * We need to catch this exception and turn it into a not found response. */ $matcher = new UrlMatcher($routeCollection, new RequestContext('/')); try { $result = $matcher->matchRequest($request); // Execute our controller and get the response $response = $result['_controller']->{$result}['_method']($result, $request); } catch (ResourceNotFoundException $exception) { $response = new Response('Page not found', 404); } // Send the response. Under the hood this uses a combination of 'echo' and 'header()' $response->send();