Esempio n. 1
0
 /**
  * Create a routing scope.
  *
  * Routing scopes allow you to keep your routes DRY and avoid repeating
  * common path prefixes, and or parameter sets.
  *
  * Scoped collections will be indexed by path for faster route parsing. If you
  * re-open or re-use a scope the connected routes will be merged with the
  * existing ones.
  *
  * ### Example
  *
  * ```
  * Router::scope('/blog', ['plugin' => 'Blog'], function ($routes) {
  *    $routes->connect('/', ['controller' => 'Articles']);
  * });
  * ```
  *
  * The above would result in a `/blog/` route being created, with both the
  * plugin & controller default parameters set.
  *
  * You can use Router::plugin() and Router::prefix() as shortcuts to creating
  * specific kinds of scopes.
  *
  * Routing scopes will inherit the globally set extensions configured with
  * Router::extensions(). You can also set valid extensions using
  * `$routes->extensions()` in your closure.
  *
  * @param string $path The path prefix for the scope. This path will be prepended
  *   to all routes connected in the scoped collection.
  * @param array|callable $params An array of routing defaults to add to each connected route.
  *   If you have no parameters, this argument can be a callable.
  * @param callable|null $callback The callback to invoke with the scoped collection.
  * @throws \InvalidArgumentException When an invalid callable is provided.
  * @return void
  */
 public static function scope($path, $params = [], $callback = null)
 {
     $builder = new RouteBuilder(static::$_collection, '/', [], ['routeClass' => static::defaultRouteClass(), 'extensions' => static::$_defaultExtensions]);
     $builder->scope($path, $params, $callback);
 }
 /**
  * Test getting named routes.
  *
  * @return void
  */
 public function testNamed()
 {
     $routes = new RouteBuilder($this->collection, '/l');
     $routes->connect('/:controller', ['action' => 'index'], ['_name' => 'cntrl']);
     $routes->connect('/:controller/:action/*');
     $all = $this->collection->named();
     $this->assertCount(1, $all);
     $this->assertInstanceOf('Cake\\Routing\\Route\\Route', $all['cntrl']);
     $this->assertEquals('/l/:controller', $all['cntrl']->template);
 }
Esempio n. 3
0
 /**
  * Test using name prefixes.
  *
  * @return void
  */
 public function testNamePrefixes()
 {
     $routes = new RouteBuilder($this->collection, '/api', [], ['namePrefix' => 'api:']);
     $routes->scope('/v1', ['version' => 1, '_namePrefix' => 'v1:'], function ($routes) {
         $this->assertEquals('api:v1:', $routes->namePrefix());
         $routes->connect('/ping', ['controller' => 'Pings'], ['_name' => 'ping']);
         $routes->namePrefix('web:');
         $routes->connect('/pong', ['controller' => 'Pongs'], ['_name' => 'pong']);
     });
     $all = $this->collection->named();
     $this->assertArrayHasKey('api:v1:ping', $all);
     $this->assertArrayHasKey('web:pong', $all);
 }
 /**
  * Test adding a scope.
  *
  * @return void
  */
 public function testScope()
 {
     $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
     $routes->scope('/v1', ['version' => 1], function ($routes) {
         $this->assertEquals('/api/v1', $routes->path());
         $this->assertEquals(['prefix' => 'api', 'version' => 1], $routes->params());
     });
     $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
     $routes->scope('/v1', function ($routes) {
         $this->assertEquals('/api/v1', $routes->path());
         $this->assertEquals(['prefix' => 'api'], $routes->params());
     });
 }