Skip to content

Commit

Permalink
Remove redudant usage forms for Router::url().
Browse files Browse the repository at this point in the history
You can no longer specify route name as string for first argument.
  • Loading branch information
ADmad committed Aug 11, 2014
1 parent 08de821 commit ae76e5e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 39 deletions.
33 changes: 8 additions & 25 deletions src/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,6 @@ protected static function _applyUrlFilters($url) {
* generated through reverse routing.
* - `Router::url(['_name' => 'custom-name', ...]);` Returns a URL generated
* through reverse routing. This form allows you to leverage named routes.
* - `Router::url('custom-name', [...]);` Returns a URL generated through reverse
* routing. This form allows you to leverage named routes.
*
* There are a few 'special' parameters that can change the final URL string that is generated
*
Expand All @@ -487,29 +485,25 @@ protected static function _applyUrlFilters($url) {
* - `_full` - If true output of `Router::fullBaseUrl()` will be prepended to generated URLs.
* - `#` - Allows you to set URL hash fragments.
* - `_ssl` - Set to true to convert the generated URL to https, or false to force http.
* - `_name` - Name of route.
* - `_name` - Name of route. If you have setup named routes you can use this key
* to specify it.
*
* @param string|array $url An array specifying any of the following:
* 'controller', 'action', 'plugin' additionally, you can provide routed
* elements or query string parameters. If string it can be name of a route or
* any valid url string.
* @param bool|array $options If (bool) true, the full base URL will be prepended to the result.
* If an array accepts the following keys. If used with a named route you can provide
* a list of query string parameters.
* elements or query string parameters. If string it can be name any valid url
* string.
* @param bool $full If true, the full base URL will be prepended to the result.
* Default is false.
* @return string Full translated URL with base path.
* @throws \Cake\Error\Exception When the route name is not found
*/
public static function url($url = null, $options = []) {
public static function url($url = null, $full = false) {
if (!static::$initialized) {
static::_loadRoutes();
}

$full = false;
if (is_bool($options)) {
list($full, $options) = array($options, []);
}
$urlType = gettype($url);
$hasLeadingSlash = $plainString = false;
$plainString = false;

if ($urlType === 'string') {
$plainString = (
Expand All @@ -522,8 +516,6 @@ public static function url($url = null, $options = []) {
strpos($url, '//') === 0 ||
strpos($url, '://') !== false
);

$hasLeadingSlash = isset($url[0]) ? $url[0] === '/' : false;
}

$params = array(
Expand Down Expand Up @@ -592,15 +584,6 @@ public static function url($url = null, $options = []) {
);
}

$url = static::_applyUrlFilters($url);
$output = static::$_collection->match($url, static::$_requestContext);
} elseif (
$urlType === 'string' &&
!$hasLeadingSlash &&
!$plainString
) {
// named route.
$url = $options + ['_name' => $url];
$url = static::_applyUrlFilters($url);
$output = static::$_collection->match($url, static::$_requestContext);
} else {
Expand Down
16 changes: 2 additions & 14 deletions tests/TestCase/Routing/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1056,30 +1056,18 @@ public function testUrlGenerationNamedRoute() {
['_name' => 'Articles::view']
);

$url = Router::url('test', array('name' => 'mark'));
$url = Router::url(['_name' => 'test', 'name' => 'mark']);
$this->assertEquals('/users/mark', $url);

$url = Router::url('test', array('name' => 'mark', 'page' => 1, 'sort' => 'title', 'dir' => 'desc'));
$this->assertEquals('/users/mark?page=1&sort=title&dir=desc', $url);

$url = Router::url([
'_name' => 'test', 'name' => 'mark',
'page' => 1, 'sort' => 'title', 'dir' => 'desc'
]);
$this->assertEquals('/users/mark?page=1&sort=title&dir=desc', $url);

$url = Router::url('users-index');
$this->assertEquals('/users', $url);

$url = Router::url('Articles::view');
$this->assertEquals('/view/', $url);

$url = Router::url(['_name' => 'Articles::view']);
$this->assertEquals('/view/', $url);

$url = Router::url('Articles::view', ['1']);
$this->assertEquals('/view/1', $url);

$url = Router::url(['_name' => 'Articles::view', '1']);
$this->assertEquals('/view/1', $url);

Expand All @@ -1102,7 +1090,7 @@ public function testNamedRouteException() {
array('controller' => 'users', 'action' => 'view'),
array('_name' => 'test')
);
$url = Router::url('junk', array('name' => 'mark'));
$url = Router::url(['_name' => 'junk', 'name' => 'mark']);
}

/**
Expand Down

0 comments on commit ae76e5e

Please sign in to comment.