Beispiel #1
0
 /**
  * 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 testPrepareNamed($sample_route, $sample_other_route, $sample_named_route)
 {
     $array_of_routes = array($sample_route, $sample_other_route, $sample_named_route);
     $route_name = $sample_named_route->getName();
     // Create our collection
     $routes = new RouteCollection($array_of_routes);
     $original_keys = $routes->keys();
     // Prepare the named routes
     $routes->prepareNamed();
     $this->assertNotSame($original_keys, $routes->keys());
     $this->assertSame(count($original_keys), count($routes->keys()));
     $this->assertSame($sample_named_route, $routes->get($route_name));
 }