Typically, Route objects are created and handled through the Router class, as follows. When connecting a route, a Route object is instantiated behind the scenes, and added to the Router's collection. Router::connect("/{:controller}/{:action}"); This following matches a set of parameters against all Route objects contained in Router, and if a match is found, returns a string URL with parameters inserted into the URL pattern. Router::match(array("controller" => "users", "action" => "login")); // returns "/users/login" For more advanced routing, however, you can directly instantiate a Route object, a subclass, or any class that implements parse() and match() (see the documentation for each individual method) and configure it manually -- if, for example, you want the route to match different incoming URLs than it generates. $route = new Route(array( 'template' => '/users/{:user}', 'pattern' => '@^/u(?:sers)?(?:/(?P[^\/]+))$@', 'params' => array('controller' => 'users', 'action' => 'index'), 'match' => array('controller' => 'users', 'action' => 'index'), 'defaults' => array('controller' => 'users'), 'keys' => array('user' => 'user'), 'options' => array('compile' => false, 'wrap' => false) )); Router::connect($route); // this will match '/users/' or '/u/'.
See also: lithium\net\http\Route::compile()
See also: lithium\net\http\Router
Inheritance: extends lithium\core\Object
Example #1
0
 public function match(array $options = array(), $context = null)
 {
     $locale = Environment::get('locale');
     return parent::match($options + compact('locale'), $context);
 }
Example #2
0
 /**
  * Tests that a single route with default values matches its default parameters, as well as
  * non-default parameters.
  */
 public function testSingleRouteWithDefaultValues()
 {
     $defaults = array('controller' => 'Admin', 'action' => 'index');
     $route = new Route(compact('defaults') + array('template' => '/{:controller}/{:action}', 'pattern' => '@^(?:/(?P[^\\/]+)?)?(?:/(?P[^\\/]+)?)?$@u', 'params' => array('controller' => 'Admin', 'action' => 'index'), 'keys' => array('controller' => 'controller', 'action' => 'action'), 'match' => array()));
     $this->assertIdentical('/', $route->match($defaults));
     $nonDefault = array('controller' => 'Admin', 'action' => 'view');
     $this->assertIdentical('/Admin/view', $route->match($nonDefault));
 }
Example #3
0
 public function testRouteParsingWithRegexActionAndParamWithoutAction()
 {
     $route = new Route(array('template' => '/products/{:action:add|edit|remove}/{:category}', 'params' => array('controller' => 'Products')));
     $request = new Request();
     $request->url = '/products/hello';
     $result = $route->parse($request);
     $this->assertEqual(false, $result);
     $request = new Request();
     $request->url = '/products';
     $result = $route->parse($request);
     $this->assertEqual(false, $result);
 }
Example #4
0
	public function testUrlEncodedArgs() {
		$route = new Route(array('template' => '/{:controller}/{:action}/{:args}'));
		$request = new Request();
		$request->url = '/posts/index/Food%20%26%20Dining';
		$result = $route->parse($request);
		$expected = array(
			'controller' => 'posts', 'action' => 'index', 'args' => array('Food%20%26%20Dining')
		);
		$this->assertEqual($expected, $result->params);
	}
Example #5
0
 /**
  * Tests fix for route parameter matching.
  */
 public function testTwoParameterRoutes()
 {
     $route = new Route(array('template' => '/personnel/{:personnel_id}/position/{:position_id}/actions/create', 'params' => array('controller' => 'actions', 'action' => 'create')));
     $route->compile();
     $data = $route->export();
     $actual = $data['pattern'];
     $expected = '@^/personnel(?:/(?P<personnel_id>[^\\/]+))/position(?:/';
     $expected .= '(?P<position_id>[^\\/]+))/actions/create$@';
     $this->assertEqual($expected, $actual);
 }
Example #6
0
 /**
  * Tests that routes with optional trailing elements have unnecessary slashes trimmed.
  *
  * @return void
  */
 public function testTrimmingEmptyPathElements()
 {
     $route = new Route(array('template' => '/{:controller}/{:id:[0-9]+}', 'params' => array('action' => 'index', 'id' => null)));
     $url = $route->match(array('controller' => 'posts', 'id' => '13'));
     $this->assertEqual("/posts/13", $url);
     $url = $route->match(array('controller' => 'posts'));
     $this->assertEqual("/posts", $url);
 }
 /**
  * Tests creating a route with a custom regex sub-pattern in a template
  *
  * @return void
  */
 public function testCustomSubPattern()
 {
     $route = new Route(array('template' => '/{:controller}/{:action}/{:user:\\d+}'));
     $request = new Request();
     $request->url = '/users/view/10';
     $expected = array('controller' => 'users', 'action' => 'view', 'user' => '10');
     $result = $route->parse($request);
     $this->assertEqual($expected, $result);
     $request->url = '/users/view/my_login';
     $result = $route->parse($request);
     $this->assertFalse($result);
 }