Skip to content

Commit

Permalink
Remove persistent parameters.
Browse files Browse the repository at this point in the history
They will be re-added through a more generic interface later.
  • Loading branch information
markstory committed Jul 4, 2012
1 parent 25c91c4 commit 605b2cc
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 123 deletions.
18 changes: 0 additions & 18 deletions lib/Cake/Routing/Route/Route.php
Expand Up @@ -319,24 +319,6 @@ protected function _parseArgs($args, $context) {
return $pass;
}

/**
* Apply persistent parameters to a url array. Persistent parameters are a special
* key used during route creation to force route parameters to persist when omitted from
* a url array.
*
* @param array $url The array to apply persistent parameters to.
* @param array $params An array of persistent values to replace persistent ones.
* @return array An array with persistent parameters applied.
*/
public function persistParams($url, $params) {
foreach ($this->options['persist'] as $persistKey) {
if (array_key_exists($persistKey, $params) && !isset($url[$persistKey])) {
$url[$persistKey] = $params[$persistKey];
}
}
return $url;
}

/**
* Attempt to match a url array. If the url matches the route parameters and settings, then
* return a generated string url. If the url doesn't match the route parameters, false will be returned.
Expand Down
15 changes: 4 additions & 11 deletions lib/Cake/Routing/RouteCollection.php
Expand Up @@ -56,42 +56,35 @@ public function add(Route $route) {
* Returns either the string URL generate by the route, or false on failure.
*
* @param array $url The url to match.
* @param array $currentParams The current request parameters, used for persistent parameters.
* @return void
* @TODO Remove persistent params? Are they even useful?
*/
public function match($url, $currentParams = array()) {
public function match($url) {
$names = $this->_getNames($url);
unset($url['_name']);
foreach ($names as $name) {
if (isset($this->_routeTable[$name])) {
$output = $this->_matchRoutes($this->_routeTable[$name], $url, $currentParams);
$output = $this->_matchRoutes($this->_routeTable[$name], $url);
if ($output) {
return $output;
}
}
}
return $this->_matchRoutes($this->_routes, $url, $currentParams);
return $this->_matchRoutes($this->_routes, $url);
}

/**
* Matches a set of routes with a given $url and $params
*
* @param array $routes An array of routes to match against.
* @param array $url The url to match.
* @param array $requestContext The current request parameters, used for persistent parameters.
* @return mixed Either false on failure, or a string on success.
*/
protected function _matchRoutes($routes, $url, $currentParams) {
protected function _matchRoutes($routes, $url) {
$output = false;
for ($i = 0, $len = count($routes); $i < $len; $i++) {
$originalUrl = $url;
$route =& $routes[$i];

if (isset($route->options['persist'], $currentParams)) {
$url = $route->persistParams($url, $currentParams);
}

if ($match = $route->match($url, $this->_requestContext)) {
$output = trim($match, '/');
break;
Expand Down
7 changes: 2 additions & 5 deletions lib/Cake/Routing/Router.php
Expand Up @@ -247,9 +247,6 @@ public static function resourceMap($resourceMap = null) {
*
* - `pass` is used to define which of the routed parameters should be shifted into the pass array. Adding a
* parameter to pass will remove it from the regular route array. Ex. `'pass' => array('slug')`
* - `persist` is used to define which route parameters should be automatically included when generating
* new urls. You can override persistent parameters by redefining them in a url or remove them by
* setting the parameter to `false`. Ex. `'persist' => array('lang')`
* - `routeClass` is used to extend and change how individual routes parse requests and handle reverse routing,
* via a custom routing class. Ex. `'routeClass' => 'SlugRoute'`
* - `_name` Used to define a specific name for routes. This can be used to optimize reverse routing lookups.
Expand Down Expand Up @@ -305,7 +302,7 @@ public static function connect($route, $defaults = array(), $options = array())
*
* Examples:
*
* `Router::redirect('/home/*', array('controller' => 'posts', 'action' => 'view', array('persist' => true)));`
* `Router::redirect('/home/*', array('controller' => 'posts', 'action' => 'view'));`
*
* Redirects /home/* to /posts/view and passes the parameters to /posts/view. Using an array as the
* redirect destination allows you to use other routes to define where a url string should be redirected to.
Expand Down Expand Up @@ -700,7 +697,7 @@ public static function url($url = null, $options = array()) {
'controller' => $params['controller'],
'plugin' => $params['plugin']
);
$output = self::$_routes->match($url, $params);
$output = self::$_routes->match($url);
} elseif (
$urlType === 'string' &&
!$hasLeadingSlash &&
Expand Down
24 changes: 0 additions & 24 deletions lib/Cake/Test/TestCase/Routing/Route/RouteTest.php
Expand Up @@ -514,30 +514,6 @@ public function testQueryStringGeneration() {
ini_set('arg_separator.output', $restore);
}

/**
* test persistParams ability to persist parameters from $params and remove params.
*
* @return void
*/
public function testPersistParams() {
$route = new Route(
'/:lang/:color/blog/:action',
array('controller' => 'posts'),
array('persist' => array('lang', 'color'))
);
$url = array('controller' => 'posts', 'action' => 'index');
$params = array('lang' => 'en', 'color' => 'blue');
$result = $route->persistParams($url, $params);
$this->assertEquals('en', $result['lang']);
$this->assertEquals('blue', $result['color']);

$url = array('controller' => 'posts', 'action' => 'index', 'color' => 'red');
$params = array('lang' => 'en', 'color' => 'blue');
$result = $route->persistParams($url, $params);
$this->assertEquals('en', $result['lang']);
$this->assertEquals('red', $result['color']);
}

/**
* test the parse method of Route.
*
Expand Down
65 changes: 0 additions & 65 deletions lib/Cake/Test/TestCase/Routing/RouterTest.php
Expand Up @@ -968,71 +968,6 @@ public function testUrlParsing() {
$this->assertEquals($expected, $result);
}

/**
* test that the persist key works.
*
* @return void
*/
public function testPersistentParameters() {
Router::reload();
Router::connect('/:controller', array('action' => 'index'));
Router::connect('/:controller/:action/*');
Router::connect(
'/:lang/:color/posts/view/*',
array('controller' => 'posts', 'action' => 'view'),
array('persist' => array('lang', 'color')
));
Router::connect(
'/:lang/:color/posts/index',
array('controller' => 'posts', 'action' => 'index'),
array('persist' => array('lang')
));
Router::connect('/:lang/:color/posts/edit/*', array('controller' => 'posts', 'action' => 'edit'));
Router::connect('/about', array('controller' => 'pages', 'action' => 'view', 'about'));

Router::parse('/en/red/posts/view/5');

$request = new Request();
Router::setRequestInfo(
$request->addParams(array(
'lang' => 'en',
'color' => 'red',
'prefix' => 'admin',
'plugin' => null,
'action' => 'view',
'controller' => 'posts',
))->addPaths(array(
'base' => '/',
'here' => '/en/red/posts/view/5',
'webroot' => '/',
))
);

$expected = '/en/red/posts/view/6';
$result = Router::url(array('controller' => 'posts', 'action' => 'view', 6));
$this->assertEquals($expected, $result);

$expected = '/en/blue/posts/index';
$result = Router::url(array('controller' => 'posts', 'action' => 'index', 'color' => 'blue'));
$this->assertEquals($expected, $result);

$expected = '/posts/edit/6';
$result = Router::url(array('controller' => 'posts', 'action' => 'edit', 6, 'color' => null, 'lang' => null));
$this->assertEquals($expected, $result);

$expected = '/posts';
$result = Router::url(array('controller' => 'posts', 'action' => 'index'));
$this->assertEquals($expected, $result);

$expected = '/posts/edit/7';
$result = Router::url(array('controller' => 'posts', 'action' => 'edit', 7));
$this->assertEquals($expected, $result);

$expected = '/about';
$result = Router::url(array('controller' => 'pages', 'action' => 'view', 'about'));
$this->assertEquals($expected, $result);
}

/**
* testUuidRoutes method
*
Expand Down

0 comments on commit 605b2cc

Please sign in to comment.