Introduce the strip argument to Router.

This allows callers to request that the basepath *not* be stripped off
when normalizing string URL's. This is important in AuthComponent when
handling redirect URL's as the redirect location could point to
a controller that shares a name with the base path.

Refs #3897
Refs #3916
This commit is contained in:
mark_story 2013-07-12 21:16:18 -04:00
parent f9fdc1e6e0
commit 52be365598
2 changed files with 5 additions and 2 deletions

View file

@ -1053,7 +1053,7 @@ class Router {
* @param array|string $url URL to normalize Either an array or a string URL. * @param array|string $url URL to normalize Either an array or a string URL.
* @return string Normalized URL * @return string Normalized URL
*/ */
public static function normalize($url = '/') { public static function normalize($url = '/', $strip = true) {
if (is_array($url)) { if (is_array($url)) {
$url = Router::url($url); $url = Router::url($url);
} }
@ -1062,7 +1062,7 @@ class Router {
} }
$request = Router::getRequest(); $request = Router::getRequest();
if (!empty($request->base) && stristr($url, $request->base)) { if ($strip && !empty($request->base) && stristr($url, $request->base)) {
$url = preg_replace('/^' . preg_quote($request->base, '/') . '/', '', $url, 1); $url = preg_replace('/^' . preg_quote($request->base, '/') . '/', '', $url, 1);
} }
$url = '/' . $url; $url = '/' . $url;

View file

@ -284,6 +284,9 @@ class RouterTest extends CakeTestCase {
$result = Router::normalize('/us/users/logout/'); $result = Router::normalize('/us/users/logout/');
$this->assertEquals('/users/logout', $result); $this->assertEquals('/users/logout', $result);
$result = Router::normalize('/us/users/logout/', false);
$this->assertEquals('/us/users/logout', $result);
Router::reload(); Router::reload();
$request = new CakeRequest(); $request = new CakeRequest();