/** * Set the routes * * @param array $routes Array of routes configurations * * @throws \Exception */ public function configureRoutes($routes) { $routeCollection = new RouteCollection(); foreach ($routes as $route) { if ($this->debug) { Log::write('Configuring route "' . $route['path'] . '"'); } $routeObject = $this->router->respond($route['httpMethod'], $route['path'], $route['callback']); $routeObject->setName($route['name']); $routeCollection->set($route['name'], $routeObject); } $this->router = new Klein($this->router->service(), $this->router->app(), $routeCollection); if ($this->debug) { // Add a catchall debugging route Log::write('Configuring catchall route'); $this->router->respond('*', function (Request $request, Response $response) { Log::write(' ==> URI called : "' . $request->uri() . '" / User Agent : "' . $request->userAgent() . '"'); Log::write(' <== Response code : ' . $response->code()); }); } }
/** * Get the path for a given route * * This looks up the route by its passed name and returns * the path/url for that route, with its URL params as * placeholders unless you pass a valid key-value pair array * of the placeholder params and their values * * If a pathname is a complex/custom regular expression, this * method will simply return the regular expression used to * match the request pathname, unless an optional boolean is * passed "flatten_regex" which will flatten the regular * expression into a simple path string * * This method, and its style of reverse-compilation, was originally * inspired by a similar effort by Gilles Bouthenot (@gbouthenot) * * @link https://github.com/gbouthenot * @param string $route_name The name of the route * @param array $params The array of placeholder fillers * @param boolean $flatten_regex Optionally flatten custom regular expressions to "/" * @throws OutOfBoundsException If the route requested doesn't exist * @access public * @return string */ public function getPathFor($route_name, array $params = null, $flatten_regex = true) { // First, grab the route $route = $this->routes->get($route_name); // Make sure we are getting a valid route if (null === $route) { throw new OutOfBoundsException('No such route with name: ' . $route_name); } $path = $route->getPath(); if (preg_match_all(static::ROUTE_COMPILE_REGEX, $path, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { list($block, $pre, $inner_block, $type, $param, $optional) = $match; if (isset($params[$param])) { $path = str_replace($block, $pre . $params[$param], $path); } elseif ($optional) { $path = str_replace($block, '', $path); } } } elseif ($flatten_regex && strpos($path, '@') === 0) { // If the path is a custom regular expression and we're "flattening", just return a slash $path = '/'; } return $path; }
/** * @dataProvider sampleDataProvider */ public function testRouteOrderDoesntChangeAfterPreparing() { // Get the provided data dynamically $array_of_routes = func_get_args(); // Set the number of times we should loop $loop_num = 10; // Loop a set number of times to check different permutations for ($i = 0; $i < $loop_num; $i++) { // Shuffle the sample routes array shuffle($array_of_routes); // Create our collection and prepare the routes $routes = new RouteCollection($array_of_routes); $routes->prepareNamed(); $this->assertSame(array_values($routes->all()), array_values($array_of_routes)); } }