Skip to content
Permalink
Browse files

Implement url filters in router.

These replace persistent parameters and give a more
general purpose system for handling application specific url
strategies or features like persistent parameters.
  • Loading branch information...
markstory committed May 20, 2012
1 parent c33ea51 commit 2d5de312a987581d3566049f91e0d538078138df
Showing with 61 additions and 9 deletions.
  1. +27 −9 lib/Cake/Routing/Router.php
  2. +34 −0 lib/Cake/Test/TestCase/Routing/RouterTest.php
@@ -136,12 +136,12 @@ class Router {
protected static $_initialState = array();
/**
* The stack of URL processors to apply against routing urls before passing the
* The stack of URL filters to apply against routing urls before passing the
* parameters to the route collection.
*
* @var array
*/
protected static $_urlProcessors = array();
protected static $_urlFilters = array();
/**
* Default route class to use
@@ -570,24 +570,24 @@ public static function promote($which = null) {
}
/**
* Add a processor to Router.
* Add a url filter to Router.
*
* Processor functions are applied to every array $url provided to
* Url filter functions are applied to every array $url provided to
* Router::url() before the urls are sent to the route collection.
*
* Callback functions should expect the following parameters:
*
* - `$params` The url params being processed.
* - `$request` The current request.
*
* The processor function should *always* return the params even if unmodified.
* The url filter function should *always* return the params even if unmodified.
*
* ### Usage
*
* Processors allow you to easily implement features like persistent parameters.
* Url filters allow you to easily implement features like persistent parameters.
*
* {{{
* Router::addProcessor(function ($params, $request) {
* Router::addUrlFilter(function ($params, $request) {
* if (isset($request->params['lang']) && !isset($params['lang']) {
* $params['lang'] = $request->params['lang'];
* }
@@ -598,8 +598,24 @@ public static function promote($which = null) {
* @param callable $function The function to add
* @return void
*/
public static addProcessor($function) {
public static function addUrlFilter($function) {
self::$_urlFilters[] = $function;
}
/**
* Applies all the connected url filters to the url.
*
* @param array $url The url array being modified.
* @return array The modified url.
* @see Router::url()
* @see Router::addUrlFilter()
*/
protected static function _applyUrlFilters($url) {
$request = self::getRequest(true);
foreach (self::$_urlFilters as $filter) {
$url = $filter($url, $request);
}
return $url;
}
/**
@@ -731,6 +747,7 @@ public static function url($url = null, $options = array()) {
'controller' => $params['controller'],
'plugin' => $params['plugin']
);
$url = self::_applyUrlFilters($url);
$output = self::$_routes->match($url);
} elseif (
$urlType === 'string' &&
@@ -749,7 +766,8 @@ public static function url($url = null, $options = array()) {
$url = $options +
$route->defaults +
array('_name' => $url);
$output = self::$_routes->match($url, $params);
$url = self::_applyUrlFilters($url);
$output = self::$_routes->match($url);
} else {
// String urls.
if (
@@ -825,6 +825,40 @@ public function testNamedRouteException() {
$url = Router::url('junk', array('name' => 'mark'));
}
/**
* Test that url filters are applied to url params.
*
* @return void
*/
public function testUrlGenerationWithUrlFilter() {
Router::connect('/:lang/:controller/:action/*');
$request = new Request();
$request->addParams(array(
'lang' => 'en',
'controller' => 'posts',
'action' => 'index'
))->addPaths(array(
'base' => '',
'here' => '/'
));
Router::pushRequest($request);
$calledCount = 0;
Router::addUrlFilter(function ($url, $request) use (&$calledCount) {
$calledCount++;
$url['lang'] = $request->lang;
return $url;
});
Router::addUrlFilter(function ($url, $request) use (&$calledCount) {
$calledCount++;
$url[] = '1234';
return $url;
});
$result = Router::url(array('controller' => 'tasks', 'action' => 'edit'));
$this->assertEquals('/en/tasks/edit/1234', $result);
$this->assertEquals(2, $calledCount);
}
/**
* test that you can leave active plugin routes with plugin = null
*

0 comments on commit 2d5de31

Please sign in to comment.
You can’t perform that action at this time.