Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Close #671

  • Loading branch information...
commit 9745164d2e947d047af29ab46cffaaae28b3ad05 1 parent 2020719
@jails jails authored
Showing with 58 additions and 9 deletions.
  1. +5 −8 net/http/Route.php
  2. +53 −1 tests/cases/net/http/RouteTest.php
View
13 net/http/Route.php
@@ -190,7 +190,7 @@ public function __construct(array $config = array()) {
protected function _init() {
parent::_init();
- if (!$this->_config['continue']) {
+ if (!$this->_config['continue'] && !preg_match('@{:action:.*?}@', $this->_template)) {
$this->_params += array('action' => 'index');
}
if (!$this->_config['pattern']) {
@@ -229,18 +229,14 @@ public function parse($request, array $options = array()) {
return false;
}
}
-
if (isset($match['args'])) {
$match['args'] = explode('/', $match['args']);
}
+ $result = array_filter(array_intersect_key($match, $this->_keys));
if (isset($this->_keys['args'])) {
- $match += array('args' => array());
- }
- $result = array_intersect_key($match, $this->_keys) + $this->_params + $this->_defaults;
-
- if (isset($result['action']) && !$result['action']) {
- $result['action'] = 'index';
+ $result += array('args' => array());
}
+ $result += $this->_params + $this->_defaults;
$request->params = $result + (array) $request->params;
$request->persist = array_unique(array_merge($request->persist, $this->_persist));
@@ -464,6 +460,7 @@ protected function _regex($regex, $param, $token, $prefix) {
} else {
$regex = '[^\/]+';
}
+
$req = $param === 'args' || array_key_exists($param, $this->_params) ? '?' : '';
if ($prefix === '/') {
View
54 tests/cases/net/http/RouteTest.php
@@ -62,7 +62,6 @@ public function testBaseRouteParsing() {
*/
public function testSimpleRouteMatching() {
$route = new Route(array('template' => '/{:controller}'));
-
$result = $route->match(array('controller' => 'posts', 'action' => 'index'));
$this->assertEqual('/posts', $result);
@@ -696,6 +695,59 @@ public function testSingleRouteWithDefaultValues() {
$nonDefault = array('controller' => 'Admin', 'action' => 'view');
$this->assertIdentical('/Admin/view', $route->match($nonDefault));
}
+
+ public function testRouteParsingWithRegexAction() {
+ $route = new Route(array(
+ 'template' => '/products/{:action:add|edit|remove}/{:category}',
+ 'params' => array('controller' => 'Products')
+ ));
+ $request = new Request();
+ $request->url = '/products/add/computer';
+ $result = $route->parse($request);
+ $expected = array(
+ 'controller' => 'Products',
+ 'action' => 'add',
+ 'category' => 'computer'
+ );
+ $this->assertEqual($expected, $result->params);
+
+ $request = new Request();
+ $request->url = '/products/index/computer';
+ $result = $route->parse($request);
+ $this->assertEqual(false, $result);
+ }
+
+ public function testRouteParsingWithRegexActionAndParamWithAction() {
+ $route = new Route(array(
+ 'template' => '/products/{:action:add|edit|remove}/{:category}',
+ 'params' => array('controller' => 'Products', 'action' => 'index')
+ ));
+ $request = new Request();
+ $request->url = '/products/hello';
+ $result = $route->parse($request);
+ $expected = array(
+ 'controller' => 'Products',
+ 'action' => 'index',
+ 'category' => 'hello'
+ );
+ $this->assertEqual($expected, $result->params);
+ }
+
+ 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);
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.