parse() public method

Attempts to parse a request object and determine its execution details.
See also: lithium\net\http\Request
See also: lithium\net\http\Request::$params
See also: lithium\net\http\Route::$_handler
public parse ( Request $request, array $options = [] ) : object | boolean
$request Request A request object containing the details of the request to be routed.
$options array Used to determine the operation of the method, and override certain values in the `Request` object: - `'url'` _string_: If present, will be used to match in place of the `$url` property of `$request`.
return object | boolean If this route matches `$request`, returns the request with execution details attached to it (inside `Request::$params`). Alternatively when a route handler function was used, returns the result of its invocation. Returns `false` if the route never matched.
Ejemplo n.º 1
0
 /**
  * Tests that routes with Unicode characters are correctly parsed.
  */
 public function testUnicodeParameters()
 {
     $route = new Route(array('template' => '/{:slug:[\\pL\\pN\\-\\%]+}', 'params' => array('controller' => 'users', 'action' => 'view')));
     $unicode = 'clément';
     $slug = rawurlencode($unicode);
     $params = array('controller' => 'users', 'action' => 'view') + compact('slug');
     $result = $route->match($params);
     $this->assertEqual("/{$slug}", $result);
     $request = new Request(array('url' => "/{$slug}"));
     $result = $route->parse($request, array('url' => $request->url));
     $expected = array('controller' => 'users', 'action' => 'view') + compact('slug');
     $this->assertEqual($expected, $result->params);
     $request = new Request(array('url' => "/{$slug}"));
     $result = $route->parse($request, array('url' => $request->url));
     $expected = array('controller' => 'users', 'action' => 'view') + compact('slug');
     $this->assertEqual($expected, $result->params);
 }
Ejemplo n.º 2
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);
 }
Ejemplo n.º 3
0
 /**
  * Tests that requests can be routed based on HTTP method verbs or HTTP headers.
  *
  * @return void
  */
 public function testHeaderAndMethodBasedRouting()
 {
     $parameters = array('controller' => 'users', 'action' => 'edit');
     $route = new Route(array('template' => '/', 'params' => $parameters + array('http:method' => 'POST')));
     $request = new Request(array('env' => array('HTTP_METHOD' => 'GET')));
     $request->url = '/';
     $this->assertFalse($route->parse($request));
     $request = new Request(array('env' => array('REQUEST_METHOD' => 'POST')));
     $request->url = '/';
     $this->assertEqual($parameters, $route->parse($request)->params);
     $route = new Route(array('template' => '/{:controller}/{:id:[0-9]+}', 'params' => $parameters + array('http:method' => array('POST', 'PUT'))));
     $request = new Request(array('env' => array('REQUEST_METHOD' => 'PUT')));
     $request->url = '/users/abc';
     $this->assertFalse($route->parse($request));
     $request->url = '/users/54';
     $this->assertEqual($parameters + array('id' => '54'), $route->parse($request)->params);
 }
Ejemplo n.º 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);
	}
Ejemplo n.º 5
0
 /**
  * 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);
 }