Updating Router method name, and adding magic route variables

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3460 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2006-09-10 17:25:44 +00:00
parent c42b793266
commit 725a2700aa
2 changed files with 16 additions and 9 deletions

View file

@ -338,7 +338,7 @@ class Dispatcher extends Object {
*/ */
function parseParams($from_url) { function parseParams($from_url) {
$Route = Router::getInstance(); $Route = Router::getInstance();
extract(Router::getMap()); extract(Router::getNamedExpressions());
include CONFIGS.'routes.php'; include CONFIGS.'routes.php';
$params = $Route->parse ($from_url); $params = $Route->parse ($from_url);

View file

@ -68,6 +68,8 @@ class Router extends Overloadable {
var $__named = array( var $__named = array(
'Action' => 'index|show|list|add|create|edit|update|remove|del|delete|new|view|item', 'Action' => 'index|show|list|add|create|edit|update|remove|del|delete|new|view|item',
'Year' => '[12][0-9]{3}', 'Year' => '[12][0-9]{3}',
'Month' => '(0[1-9]|1[012])',
'Day' => '(0[1-9]|[12][0-9]|3[01])',
'ID' => '[0-9]+' 'ID' => '[0-9]+'
); );
/** /**
@ -103,7 +105,7 @@ class Router extends Overloadable {
* *
* @return array * @return array
*/ */
function getMap() { function getNamedExpressions() {
$_this =& Router::getInstance(); $_this =& Router::getInstance();
return $_this->__named; return $_this->__named;
} }
@ -112,11 +114,12 @@ class Router extends Overloadable {
* *
* @param string $route An empty string, or a route string "/" * @param string $route An empty string, or a route string "/"
* @param array $default NULL or an array describing the default route * @param array $default NULL or an array describing the default route
* @param array $requirements An array matching the named elements in the route to regular expressions which that element should match. * @param array $params An array matching the named elements in the route to regular expressions which that element should match.
* @param array $required A list of named elements (from $params) which are required to appear in the URL
* @see routes * @see routes
* @return array Array of routes * @return array Array of routes
*/ */
function connect($route, $default = null, $requirements = array()) { function connect($route, $default = null, $params = array(), $required = array()) {
$_this =& Router::getInstance(); $_this =& Router::getInstance();
$parsed = $names = array(); $parsed = $names = array();
@ -150,10 +153,14 @@ class Router extends Overloadable {
} }
foreach($elements as $element) { foreach($elements as $element) {
$q = '?';
if (preg_match('/^:(.+)$/', $element, $r)) { if (preg_match('/^:(.+)$/', $element, $r)) {
if (isset($requirements[$r[1]])) { if (isset($params[$r[1]])) {
$parsed[] = '(?:\/(' . $requirements[$r[1]] . '))'; if ((!empty($required) && in_array($r[1], $required)) || empty($required)) {
$q = null;
}
$parsed[] = '(?:\/(' . $params[$r[1]] . '))' . $q;
} else { } else {
$parsed[] = '(?:\/([^\/]+))?'; $parsed[] = '(?:\/([^\/]+))?';
} }