Skip to content
Permalink
Browse files

Add Route::getName()

Used to get/generate route names used to create hashtable entries.
  • Loading branch information...
markstory committed Apr 9, 2012
1 parent f001ddb commit c35f8cf9d14c06a393b6c86ca35aa0f2ad3ab67c
Showing with 77 additions and 2 deletions.
  1. +36 −2 lib/Cake/Routing/Route/Route.php
  2. +41 −0 lib/Cake/Test/TestCase/Routing/Route/RouteTest.php
@@ -14,7 +14,6 @@
namespace Cake\Routing\Route;
use Cake\Routing\Router;
use Cake\Utility\Hash;
/**
* A single Route used by the Router to connect requests to
@@ -71,6 +70,13 @@ class Route {
*/
protected $_compiledRoute = null;
/**
* The name for a route. Fetch with Route::getName();
*
* @var string
*/
protected $_name = null;
/**
* HTTP header shortcut map. Used for evaluating header-based route expressions.
*
@@ -165,12 +171,40 @@ protected function _writeRoute() {
$this->_compiledRoute = '#^' . $parsed . '[/]*$#';
$this->keys = $names;
//remove defaults that are also keys. They can cause match failures
// remove defaults that are also keys. They can cause match failures
foreach ($this->keys as $key) {
unset($this->defaults[$key]);
}
}
/**
* Get the standardized plugin.controller::action name
* for a route. This will compile a route if it has not
* already been compiled.
*
* @return string.
*/
public function getName() {
if (empty($this->_name)) {
$this->compile();
}
$name = '';
if (isset($this->defaults['plugin'])) {
$name .= strtolower($this->defaults['plugin']) . '.';
}
$parts = array();
foreach (array('controller', 'action') as $key) {
if (isset($this->defaults[$key])) {
$parts[] = $this->defaults[$key];
}
if (in_array($key, $this->keys, true)) {
$parts[] = '_' . $key;
}
}
$name .= strtolower(implode('::', $parts));
return $this->_name = $name;
}
/**
* Checks to see if the given URL can be parsed by this route.
* If the route can be parsed an array of parameters will be returned; if not
@@ -609,4 +609,45 @@ public function testParseTrailingUTF8() {
$this->assertEquals($expected, $result);
}
/**
* test getName();
*
* @return void
*/
public function testGetName() {
$route = new Route('/:controller/:action');
$this->assertEquals('_controller::_action', $route->getName());
$route = new Route('/articles/:action', array('controller' => 'posts'));
$this->assertEquals('posts::_action', $route->getName());
$route = new Route('/articles/list', array('controller' => 'posts', 'action' => 'index'));
$this->assertEquals('posts::index', $route->getName());
}
/**
* Test getName() with plugins.
*
* @return void
*/
public function testGetNamePlugins() {
$route = new Route(
'/a/:controller/:action',
array('plugin' => 'asset')
);
$this->assertEquals('asset._controller::_action', $route->getName());
$route = new Route(
'/a/assets/:action',
array('plugin' => 'asset', 'controller' => 'assets')
);
$this->assertEquals('asset.assets::_action', $route->getName());
$route = new Route(
'/assets/get',
array('plugin' => 'asset', 'controller' => 'assets', 'action' => 'get')
);
$this->assertEquals('asset.assets::get', $route->getName());
}
}

0 comments on commit c35f8cf

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