public parse ( |
||
$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`. |
Résultat | 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. |
public function parse($request, array $options = array())
{
$defaults = array('url' => $request->url);
$options += $defaults;
$url = '/' . trim($options['url'], '/');
$pattern = $this->_pattern;
if (!preg_match($pattern, $url, $match)) {
return false;
}
foreach ($this->_meta as $key => $compare) {
$value = $request->get($key);
if (!($compare == $value || is_array($compare) && in_array($value, $compare))) {
return false;
}
}
foreach ($this->_config['modifiers'] as $key => $modifier) {
if (isset($match[$key])) {
$match[$key] = $modifier($match[$key]);
}
}
$result = array_intersect_key($match + array('args' => array()), $this->_keys);
foreach ($result as $key => $value) {
if ($value === '') {
unset($result[$key]);
}
}
$result += $this->_params + $this->_defaults;
$request->params = $result + (array) $request->params;
$request->persist = array_unique(array_merge($request->persist, $this->_persist));
if ($this->_handler) {
$handler = $this->_handler;
return $handler($request);
}
return $request;
}
/** * 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); }