Implement trailing greedy star star.

Trailing args grabs all trailing arguments as a single passed
parameter.  This is useful when you want to have trailing arguments
that contain / in them.
This commit is contained in:
mark_story 2011-10-23 21:23:06 -04:00
parent d6f7669f54
commit c3f647217d
2 changed files with 35 additions and 1 deletions

View file

@ -148,7 +148,10 @@ class CakeRoute {
} }
$names[] = $name; $names[] = $name;
} }
if (preg_match('#\/\*$#', $route)) { if (preg_match('#\/\*\*$#', $route)) {
$parsed = preg_replace('#/\\\\\*\\\\\*$#', '(?:/(?P<_trailing_>.*))?', $parsed);
$this->_greedy = true;
} elseif (preg_match('#\/\*$#', $route)) {
$parsed = preg_replace('#/\\\\\*$#', '(?:/(?P<_args_>.*))?', $parsed); $parsed = preg_replace('#/\\\\\*$#', '(?:/(?P<_args_>.*))?', $parsed);
$this->_greedy = true; $this->_greedy = true;
} }
@ -226,6 +229,11 @@ class CakeRoute {
unset($route['_args_']); unset($route['_args_']);
} }
if (isset($route['_trailing_'])) {
$route['pass'][] = $route['_trailing_'];
unset($route['_trailing_']);
}
// restructure 'pass' key route params // restructure 'pass' key route params
if (isset($this->options['pass'])) { if (isset($this->options['pass'])) {
$j = count($this->options['pass']); $j = count($this->options['pass']);

View file

@ -718,4 +718,30 @@ class CakeRouteTest extends CakeTestCase {
); );
$this->assertEquals($expected, $result, 'Slug should have moved'); $this->assertEquals($expected, $result, 'Slug should have moved');
} }
/**
* Test the /** special type on parsing.
*
* @return void
*/
public function testParseTrailing() {
$route = new CakeRoute('/:controller/:action/**');
$result = $route->parse('/posts/index/1/2/3/foo:bar');
$expected = array(
'controller' => 'posts',
'action' => 'index',
'pass' => array('1/2/3/foo:bar'),
'named' => array()
);
$this->assertEquals($expected, $result);
$result = $route->parse('/posts/index/http://example.com');
$expected = array(
'controller' => 'posts',
'action' => 'index',
'pass' => array('http://example.com'),
'named' => array()
);
$this->assertEquals($expected, $result);
}
} }