Skip to content

Commit

Permalink
add urlOrNull method in router and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Elias De Vos committed Jan 16, 2018
1 parent 7b86c06 commit c5934fa
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/Routing/Router.php
Expand Up @@ -682,6 +682,54 @@ public static function url($url = null, $full = false)
return $output . $frag;
}

/**
* Finds URL for specified action.
*
* Returns a URL pointing to a combination of controller and action.
*
* If the url doesn't exist returns null
*
* ### Usage
*
* - `Router::url('/posts/edit/1');` Returns the string with the base dir prepended.
* This usage does not use reverser routing.
* - `Router::url(['controller' => 'posts', 'action' => 'edit']);` Returns a 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.
*
* There are a few 'special' parameters that can change the final URL string that is generated
*
* - `_base` - Set to false to remove the base path from the generated URL. If your application
* is not in the root directory, this can be used to generate URLs that are 'cake relative'.
* cake relative URLs are required when using requestAction.
* - `_scheme` - Set to create links on different schemes like `webcal` or `ftp`. Defaults
* to the current scheme.
* - `_host` - Set the host to use for the link. Defaults to the current host.
* - `_port` - Set the port if you need to create links on non-standard ports.
* - `_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. If you have setup named routes you can use this key
* to specify it.
*
* @param string|array|null $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 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 or null if url does not exist
*/
public static function urlOrNull($url = null, $full = false)
{
try {
return static::url($url, $full);
} catch (\Cake\Routing\Exception\MissingRouteException $e) {
return null;
}
}

/**
* Sets the full base URL that will be used as a prefix for generating
* fully qualified URLs for this application. If not parameters are passed,
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/Routing/RouterTest.php
Expand Up @@ -129,6 +129,19 @@ public function testRouteDefaultParams()
$this->assertEquals(Router::url(['action' => 'index']), '/');
}

/**
* testRouteUrlOrNull method
*
* @return void
*/
public function testRouteUrlOrNull()
{
Router::connect('/:controller/:action', ['controller' => 'posts']);
$this->assertEquals(Router::urlOrNull(['action' => 'view']), '/view');

$this->assertEquals(Router::urlOrNull(['action' => 'view', 'controller' => 'users', 'plugin' => 'test']), null);
}

/**
* testMapResources method
*
Expand Down

0 comments on commit c5934fa

Please sign in to comment.