From 725a2700aad0ba0e5d8f89ec25e5246050d40310 Mon Sep 17 00:00:00 2001 From: nate Date: Sun, 10 Sep 2006 17:25:44 +0000 Subject: [PATCH] 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 --- cake/dispatcher.php | 2 +- cake/libs/router.php | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/cake/dispatcher.php b/cake/dispatcher.php index 27ad79aca..0cf045bb2 100644 --- a/cake/dispatcher.php +++ b/cake/dispatcher.php @@ -338,7 +338,7 @@ class Dispatcher extends Object { */ function parseParams($from_url) { $Route = Router::getInstance(); - extract(Router::getMap()); + extract(Router::getNamedExpressions()); include CONFIGS.'routes.php'; $params = $Route->parse ($from_url); diff --git a/cake/libs/router.php b/cake/libs/router.php index 7b5fa06b7..92a40ec44 100644 --- a/cake/libs/router.php +++ b/cake/libs/router.php @@ -66,9 +66,11 @@ class Router extends Overloadable { * @var array */ var $__named = array( - 'Action' => 'index|show|list|add|create|edit|update|remove|del|delete|new|view|item', - 'Year' => '[12][0-9]{3}', - 'ID' => '[0-9]+' + 'Action' => 'index|show|list|add|create|edit|update|remove|del|delete|new|view|item', + 'Year' => '[12][0-9]{3}', + 'Month' => '(0[1-9]|1[012])', + 'Day' => '(0[1-9]|[12][0-9]|3[01])', + 'ID' => '[0-9]+' ); /** * Initialize the Router object @@ -103,7 +105,7 @@ class Router extends Overloadable { * * @return array */ - function getMap() { + function getNamedExpressions() { $_this =& Router::getInstance(); return $_this->__named; } @@ -112,11 +114,12 @@ class Router extends Overloadable { * * @param string $route An empty string, or a route string "/" * @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 * @return array Array of routes */ - function connect($route, $default = null, $requirements = array()) { + function connect($route, $default = null, $params = array(), $required = array()) { $_this =& Router::getInstance(); $parsed = $names = array(); @@ -150,10 +153,14 @@ class Router extends Overloadable { } foreach($elements as $element) { + $q = '?'; if (preg_match('/^:(.+)$/', $element, $r)) { - if (isset($requirements[$r[1]])) { - $parsed[] = '(?:\/(' . $requirements[$r[1]] . '))'; + if (isset($params[$r[1]])) { + if ((!empty($required) && in_array($r[1], $required)) || empty($required)) { + $q = null; + } + $parsed[] = '(?:\/(' . $params[$r[1]] . '))' . $q; } else { $parsed[] = '(?:\/([^\/]+))?'; }