Browse files

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.
  • Loading branch information...
1 parent d6f7669 commit c3f647217d305a2ffc4dc63c01f3ec5692b3d4fa @markstory markstory committed Oct 23, 2011
Showing with 35 additions and 1 deletion.
  1. +9 −1 lib/Cake/Routing/Route/CakeRoute.php
  2. +26 −0 lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php
View
10 lib/Cake/Routing/Route/CakeRoute.php
@@ -148,7 +148,10 @@ protected function _writeRoute() {
}
$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);
$this->_greedy = true;
}
@@ -226,6 +229,11 @@ public function parse($url) {
unset($route['_args_']);
}
+ if (isset($route['_trailing_'])) {
+ $route['pass'][] = $route['_trailing_'];
+ unset($route['_trailing_']);
+ }
+
// restructure 'pass' key route params
if (isset($this->options['pass'])) {
$j = count($this->options['pass']);
View
26 lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php
@@ -718,4 +718,30 @@ public function testPassArgRestructure() {
);
$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);
+ }
}

0 comments on commit c3f6472

Please sign in to comment.