2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Parses the request URL into controller, action, and parameters.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
|
|
|
* @since CakePHP(tm) v 0.2.9
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Parses the request URL into controller, action, and parameters.
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-09-27 02:24:24 +00:00
|
|
|
class Router {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 20:37:55 +00:00
|
|
|
* Array of routes connected with Router::connect()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $routes = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-09-27 04:12:03 +00:00
|
|
|
* List of action prefixes used in connected routes.
|
|
|
|
* Includes admin prefix
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__prefixes = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Directive for Router to parse out file extensions for mapping to Content-types.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__parseExtensions = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* List of valid extensions to parse from a URL. If null, any extension is allowed.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__validExtensions = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* 'Constant' regular expression definitions for named route elements
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__named = array(
|
2008-05-30 11:40:08 +00:00
|
|
|
'Action' => 'index|show|add|create|edit|update|remove|del|delete|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]+',
|
|
|
|
'UUID' => '[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}'
|
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Stores all information necessary to decide what named arguments are parsed under what conditions.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $named = array(
|
2008-05-30 11:40:08 +00:00
|
|
|
'default' => array('page', 'fields', 'order', 'limit', 'recursive', 'sort', 'direction', 'step'),
|
|
|
|
'greedy' => true,
|
|
|
|
'separator' => ':',
|
|
|
|
'rules' => false,
|
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The route matching the URL of the current request
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__currentRoute = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Default HTTP request method => controller action map.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__resourceMap = array(
|
2008-05-30 11:40:08 +00:00
|
|
|
array('action' => 'index', 'method' => 'GET', 'id' => false),
|
|
|
|
array('action' => 'view', 'method' => 'GET', 'id' => true),
|
|
|
|
array('action' => 'add', 'method' => 'POST', 'id' => false),
|
|
|
|
array('action' => 'edit', 'method' => 'PUT', 'id' => true),
|
|
|
|
array('action' => 'delete', 'method' => 'DELETE', 'id' => true),
|
|
|
|
array('action' => 'edit', 'method' => 'POST', 'id' => true)
|
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* List of resource-mapped controllers
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__resourceMapped = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Maintains the parameter stack for the current request
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__params = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Maintains the path stack for the current request
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__paths = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Keeps Router state to determine if default routes have already been connected
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__defaultsMapped = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-11-26 04:21:10 +00:00
|
|
|
/**
|
|
|
|
* Keeps track of whether the connection of default routes is enabled or disabled.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__connectDefaults = true;
|
2009-11-26 04:21:10 +00:00
|
|
|
|
2009-09-27 02:37:19 +00:00
|
|
|
/**
|
|
|
|
* Constructor for Router.
|
|
|
|
* Builds __prefixes
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-09-27 02:37:19 +00:00
|
|
|
function Router() {
|
|
|
|
$this->__setPrefixes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-09-27 04:12:03 +00:00
|
|
|
* Sets the Routing prefixes. Includes compatibilty for existing Routing.admin
|
|
|
|
* configurations.
|
2009-09-27 02:37:19 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @access private
|
2009-09-27 04:12:03 +00:00
|
|
|
* @todo Remove support for Routing.admin in future versions.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-09-27 02:37:19 +00:00
|
|
|
function __setPrefixes() {
|
|
|
|
$routing = Configure::read('Routing');
|
2009-09-30 04:50:15 +00:00
|
|
|
if (!empty($routing['admin'])) {
|
2009-09-27 04:12:03 +00:00
|
|
|
$this->__prefixes[] = $routing['admin'];
|
2009-09-27 02:37:19 +00:00
|
|
|
}
|
2009-09-30 04:50:15 +00:00
|
|
|
if (!empty($routing['prefixes'])) {
|
2009-09-27 02:48:57 +00:00
|
|
|
$this->__prefixes = array_merge($this->__prefixes, (array)$routing['prefixes']);
|
2009-09-27 02:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-18 11:28:03 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets a reference to the Router object instance
|
|
|
|
*
|
2010-01-25 20:37:55 +00:00
|
|
|
* @return Router Instance of the Router.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function &getInstance() {
|
|
|
|
static $instance = array();
|
|
|
|
|
2008-09-12 05:11:34 +00:00
|
|
|
if (!$instance) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$instance[0] =& new Router();
|
|
|
|
}
|
|
|
|
return $instance[0];
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets the named route elements for use in app/config/routes.php
|
|
|
|
*
|
|
|
|
* @return array Named route elements
|
|
|
|
* @access public
|
|
|
|
* @see Router::$__named
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function getNamedExpressions() {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
|
|
|
return $self->__named;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-11-29 04:37:40 +00:00
|
|
|
* Connects a new Route in the router.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-29 04:37:40 +00:00
|
|
|
* Routes are a way of connecting request urls to objects in your application. At their core routes
|
|
|
|
* are a set or regular expressions that are used to match requests to destinations.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
*
|
|
|
|
* `Router::connect('/:controller/:action/*');`
|
2009-12-09 02:36:25 +00:00
|
|
|
*
|
2009-11-29 04:37:40 +00:00
|
|
|
* The first parameter will be used as a controller name while the second is used as the action name.
|
|
|
|
* the '/*' syntax makes this route greedy in that it will match requests like `/posts/index` as well as requests
|
2009-12-09 02:36:25 +00:00
|
|
|
* like `/posts/edit/1/foo/bar`.
|
2009-11-29 04:37:40 +00:00
|
|
|
*
|
|
|
|
* `Router::connect('/home-page', array('controller' => 'pages', 'action' => 'display', 'home'));`
|
2009-12-09 02:36:25 +00:00
|
|
|
*
|
2009-11-29 04:37:40 +00:00
|
|
|
* The above shows the use of route parameter defaults. And providing routing parameters for a static route.
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* Router::connect(
|
|
|
|
* '/:lang/:controller/:action/:id',
|
2009-11-30 05:23:41 +00:00
|
|
|
* array(),
|
2009-11-29 04:37:40 +00:00
|
|
|
* array('id' => '[0-9]+', 'lang' => '[a-z]{3}')
|
|
|
|
* );
|
|
|
|
* }}}
|
|
|
|
*
|
|
|
|
* Shows connecting a route with custom route parameters as well as providing patterns for those parameters.
|
|
|
|
* Patterns for routing parameters do not need capturing groups, as one will be added for each route params.
|
|
|
|
*
|
2010-01-25 20:37:55 +00:00
|
|
|
* $options offers two 'special' keys. `pass` and `persist` have special meaning in the $options array.
|
|
|
|
*
|
|
|
|
* `pass` is used to define which of the routed parameters should be shifted into the pass array. Adding a
|
|
|
|
* parameter to pass will remove it from the regular route array. Ex. `'pass' => array('slug')`
|
|
|
|
*
|
|
|
|
* `persist` is used to define which route parameters should be automatically included when generating
|
|
|
|
* new urls. You can override peristent parameters by redifining them in a url or remove them by
|
|
|
|
* setting the parameter to `false`. Ex. `'persist' => array('lang')`
|
|
|
|
*
|
2009-11-29 04:37:40 +00:00
|
|
|
* @param string $route A string describing the template of the route
|
2009-12-04 01:32:28 +00:00
|
|
|
* @param array $defaults An array describing the default route parameters. These parameters will be used by default
|
2009-11-29 04:37:40 +00:00
|
|
|
* and can supply routing parameters that are not dynamic. See above.
|
2009-12-09 02:36:25 +00:00
|
|
|
* @param array $options An array matching the named elements in the route to regular expressions which that
|
|
|
|
* element should match. Also contains additional parameters such as which routed parameters should be
|
2009-11-29 04:37:40 +00:00
|
|
|
* shifted into the passed arguments. As well as supplying patterns for routing parameters.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see routes
|
2009-09-27 04:12:03 +00:00
|
|
|
* @return array Array of routes
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
2009-12-04 01:32:28 +00:00
|
|
|
function connect($route, $defaults = array(), $options = array()) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-11-26 04:40:52 +00:00
|
|
|
foreach ($self->__prefixes as $prefix) {
|
2009-12-04 01:32:28 +00:00
|
|
|
if (isset($defaults[$prefix])) {
|
|
|
|
$defaults['prefix'] = $prefix;
|
2009-11-14 21:32:18 +00:00
|
|
|
break;
|
2009-09-27 04:12:03 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-12-04 01:32:28 +00:00
|
|
|
if (isset($defaults['prefix'])) {
|
|
|
|
$self->__prefixes[] = $defaults['prefix'];
|
2009-11-26 04:40:52 +00:00
|
|
|
$self->__prefixes = array_keys(array_flip($self->__prefixes));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-12-16 20:04:59 +00:00
|
|
|
$defaults += array('action' => 'index', 'plugin' => null);
|
2009-12-04 02:20:08 +00:00
|
|
|
$routeClass = 'CakeRoute';
|
2009-12-04 01:50:13 +00:00
|
|
|
if (isset($options['routeClass'])) {
|
|
|
|
$routeClass = $options['routeClass'];
|
|
|
|
unset($options['routeClass']);
|
|
|
|
}
|
2010-01-18 15:05:23 +00:00
|
|
|
//TODO 2.0 refactor this to use a string class name, throw exception, and then construct.
|
2009-12-04 02:29:02 +00:00
|
|
|
$Route =& new $routeClass($route, $defaults, $options);
|
|
|
|
if ($routeClass !== 'CakeRoute' && !is_subclass_of($Route, 'CakeRoute')) {
|
|
|
|
trigger_error(__('Route classes must extend CakeRoute', true), E_USER_WARNING);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$self->routes[] =& $Route;
|
2009-11-26 04:40:52 +00:00
|
|
|
return $self->routes;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-09 20:03:02 +00:00
|
|
|
* Specifies what named parameters CakePHP should be parsing. The most common setups are:
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Do not parse any named parameters:
|
2010-01-25 20:37:55 +00:00
|
|
|
*
|
2009-02-04 05:00:59 +00:00
|
|
|
* {{{ Router::connectNamed(false); }}}
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Parse only default parameters used for CakePHP's pagination:
|
2010-01-25 20:37:55 +00:00
|
|
|
*
|
2009-02-04 05:00:59 +00:00
|
|
|
* {{{ Router::connectNamed(false, array('default' => true)); }}}
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Parse only the page parameter if its value is a number:
|
2010-01-25 20:37:55 +00:00
|
|
|
*
|
2009-02-04 05:00:59 +00:00
|
|
|
* {{{ Router::connectNamed(array('page' => '[\d]+'), array('default' => false, 'greedy' => false)); }}}
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Parse only the page parameter no mater what.
|
2010-01-25 20:37:55 +00:00
|
|
|
*
|
2009-02-04 05:00:59 +00:00
|
|
|
* {{{ Router::connectNamed(array('page'), array('default' => false, 'greedy' => false)); }}}
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Parse only the page parameter if the current action is 'index'.
|
2010-01-25 20:37:55 +00:00
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* Router::connectNamed(
|
|
|
|
* array('page' => array('action' => 'index')),
|
|
|
|
* array('default' => false, 'greedy' => false)
|
|
|
|
* );
|
|
|
|
* }}}
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Parse only the page parameter if the current action is 'index' and the controller is 'pages'.
|
|
|
|
*
|
2010-01-25 20:37:55 +00:00
|
|
|
* {{{
|
|
|
|
* Router::connectNamed(
|
|
|
|
* array('page' => array('action' => 'index', 'controller' => 'pages')),
|
|
|
|
* array('default' => false, 'greedy' => false)
|
|
|
|
* );
|
|
|
|
* }}}
|
|
|
|
*
|
|
|
|
* @param array $named A list of named parameters. Key value pairs are accepted where values are
|
|
|
|
* either regex strings to match, or arrays as seen above.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param array $options Allows to control all settings: separator, greedy, reset, default
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return array
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function connectNamed($named, $options = array()) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
2008-06-11 08:54:27 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($options['argSeparator'])) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self->named['separator'] = $options['argSeparator'];
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($options['argSeparator']);
|
|
|
|
}
|
2008-06-11 08:54:27 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($named === true || $named === false) {
|
|
|
|
$options = array_merge(array('default' => $named, 'reset' => true, 'greedy' => $named), $options);
|
|
|
|
$named = array();
|
2009-12-02 06:10:50 +00:00
|
|
|
} else {
|
|
|
|
$options = array_merge(array('default' => false, 'reset' => false, 'greedy' => true), $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-06-11 08:54:27 +00:00
|
|
|
|
2009-11-26 04:40:52 +00:00
|
|
|
if ($options['reset'] == true || $self->named['rules'] === false) {
|
|
|
|
$self->named['rules'] = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-06-11 08:54:27 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($options['default']) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$named = array_merge($named, $self->named['default']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($named as $key => $val) {
|
|
|
|
if (is_numeric($key)) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self->named['rules'][$val] = true;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self->named['rules'][$key] = $val;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-26 04:40:52 +00:00
|
|
|
$self->named['greedy'] = $options['greedy'];
|
|
|
|
return $self->named;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-11-26 04:21:10 +00:00
|
|
|
/**
|
|
|
|
* Tell router to connect or not connect the default routes.
|
2009-11-26 04:39:07 +00:00
|
|
|
*
|
2009-11-26 04:21:10 +00:00
|
|
|
* If default routes are disabled all automatic route generation will be disabled
|
|
|
|
* and you will need to manually configure all the routes you want.
|
|
|
|
*
|
|
|
|
* @param boolean $connect Set to true or false depending on whether you want or don't want default routes.
|
|
|
|
* @return void
|
2010-01-26 19:10:58 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
2009-11-26 04:21:10 +00:00
|
|
|
*/
|
2009-11-26 04:39:07 +00:00
|
|
|
function defaults($connect = true) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
|
|
|
$self->__connectDefaults = $connect;
|
2009-11-26 04:21:10 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates REST resource routes for the given controller(s)
|
2009-02-17 18:26:37 +00:00
|
|
|
*
|
2010-01-25 20:37:55 +00:00
|
|
|
* ### Options:
|
2009-02-17 18:26:37 +00:00
|
|
|
*
|
2009-02-04 05:00:59 +00:00
|
|
|
* - 'id' - The regular expression fragment to use when matching IDs. By default, matches
|
|
|
|
* integer values and UUIDs.
|
|
|
|
* - 'prefix' - URL prefix to use for the generated routes. Defaults to '/'.
|
2009-02-17 18:26:37 +00:00
|
|
|
*
|
2009-02-04 05:00:59 +00:00
|
|
|
* @param mixed $controller A controller name or array of controller names (i.e. "Posts" or "ListItems")
|
|
|
|
* @param array $options Options to use when generating REST routes
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function mapResources($controller, $options = array()) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
|
|
|
$options = array_merge(array('prefix' => '/', 'id' => $self->__named['ID'] . '|' . $self->__named['UUID']), $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
$prefix = $options['prefix'];
|
|
|
|
|
|
|
|
foreach ((array)$controller as $ctlName) {
|
|
|
|
$urlName = Inflector::underscore($ctlName);
|
2008-06-11 08:54:27 +00:00
|
|
|
|
2009-11-26 04:40:52 +00:00
|
|
|
foreach ($self->__resourceMap as $params) {
|
2008-05-30 11:40:08 +00:00
|
|
|
extract($params);
|
2008-08-22 04:06:29 +00:00
|
|
|
$url = $prefix . $urlName . (($id) ? '/:id' : '');
|
|
|
|
|
2008-07-30 20:34:01 +00:00
|
|
|
Router::connect($url,
|
2008-05-30 11:40:08 +00:00
|
|
|
array('controller' => $urlName, 'action' => $action, '[method]' => $params['method']),
|
|
|
|
array('id' => $options['id'], 'pass' => array('id'))
|
|
|
|
);
|
|
|
|
}
|
2009-11-26 04:40:52 +00:00
|
|
|
$self->__resourceMapped[] = $urlName;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the list of prefixes used in connected routes
|
|
|
|
*
|
|
|
|
* @return array A list of prefixes used in connected routes
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function prefixes() {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
|
|
|
return $self->__prefixes;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 20:37:55 +00:00
|
|
|
* Parses given URL and returns an array of controller, action and parameters
|
2008-05-30 11:40:08 +00:00
|
|
|
* taken from that URL.
|
|
|
|
*
|
|
|
|
* @param string $url URL to be parsed
|
|
|
|
* @return array Parsed elements from URL
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function parse($url) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
|
|
|
if (!$self->__defaultsMapped && $self->__connectDefaults) {
|
|
|
|
$self->__connectDefaultRoutes();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-12-16 20:04:59 +00:00
|
|
|
$out = array(
|
|
|
|
'pass' => array(),
|
|
|
|
'named' => array(),
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
$r = $ext = null;
|
|
|
|
|
2008-10-21 12:15:28 +00:00
|
|
|
if (ini_get('magic_quotes_gpc') === '1') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$url = stripslashes_deep($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($url && strpos($url, '/') !== 0) {
|
|
|
|
$url = '/' . $url;
|
|
|
|
}
|
|
|
|
if (strpos($url, '?') !== false) {
|
|
|
|
$url = substr($url, 0, strpos($url, '?'));
|
|
|
|
}
|
2009-11-26 04:40:52 +00:00
|
|
|
extract($self->__parseExtension($url));
|
2008-08-22 04:06:29 +00:00
|
|
|
|
2009-11-26 04:40:52 +00:00
|
|
|
for ($i = 0, $len = count($self->routes); $i < $len; $i++) {
|
|
|
|
$route =& $self->routes[$i];
|
2009-10-02 05:36:27 +00:00
|
|
|
if (($r = $route->parse($url)) !== false) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self->__currentRoute[] =& $route;
|
2009-11-05 04:05:25 +00:00
|
|
|
|
2009-12-02 05:20:51 +00:00
|
|
|
$params = $route->options;
|
2008-05-30 11:40:08 +00:00
|
|
|
$argOptions = array();
|
2008-09-17 15:26:31 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (array_key_exists('named', $params)) {
|
|
|
|
$argOptions['named'] = $params['named'];
|
|
|
|
unset($params['named']);
|
|
|
|
}
|
|
|
|
if (array_key_exists('greedy', $params)) {
|
|
|
|
$argOptions['greedy'] = $params['greedy'];
|
|
|
|
unset($params['greedy']);
|
|
|
|
}
|
2009-11-28 14:40:15 +00:00
|
|
|
$out = $r;
|
|
|
|
|
|
|
|
if (isset($out['_args_'])) {
|
|
|
|
$argOptions['context'] = array('action' => $out['action'], 'controller' => $out['controller']);
|
|
|
|
$parsedArgs = $self->getArgs($out['_args_'], $argOptions);
|
|
|
|
$out['pass'] = array_merge($out['pass'], $parsedArgs['pass']);
|
|
|
|
$out['named'] = $parsedArgs['named'];
|
|
|
|
unset($out['_args_']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($params['pass'])) {
|
2009-11-29 02:58:02 +00:00
|
|
|
$j = count($params['pass']);
|
|
|
|
while($j--) {
|
2008-08-29 18:17:14 +00:00
|
|
|
if (isset($out[$params['pass'][$j]])) {
|
|
|
|
array_unshift($out['pass'], $out[$params['pass'][$j]]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-20 20:57:01 +00:00
|
|
|
if (!empty($ext) && !isset($out['url']['ext'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$out['url']['ext'] = $ext;
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Parses a file extension out of a URL, if Router::parseExtensions() is enabled.
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @return array Returns an array containing the altered URL and the parsed extension.
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __parseExtension($url) {
|
|
|
|
$ext = null;
|
|
|
|
|
2008-10-12 03:52:24 +00:00
|
|
|
if ($this->__parseExtensions) {
|
2008-09-14 17:17:49 +00:00
|
|
|
if (preg_match('/\.[0-9a-zA-Z]*$/', $url, $match) === 1) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$match = substr($match[0], 1);
|
2008-10-12 03:52:24 +00:00
|
|
|
if (empty($this->__validExtensions)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$url = substr($url, 0, strpos($url, '.' . $match));
|
|
|
|
$ext = $match;
|
|
|
|
} else {
|
2008-10-12 03:52:24 +00:00
|
|
|
foreach ($this->__validExtensions as $name) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (strcasecmp($name, $match) === 0) {
|
|
|
|
$url = substr($url, 0, strpos($url, '.' . $name));
|
|
|
|
$ext = $match;
|
2009-11-14 21:29:33 +00:00
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($ext)) {
|
|
|
|
$ext = 'html';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return compact('ext', 'url');
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 20:37:55 +00:00
|
|
|
* Connects the default, built-in routes, including prefix and plugin routes. The following routes are created
|
|
|
|
* in the order below:
|
|
|
|
*
|
|
|
|
* - `/:prefix/:plugin/:controller`
|
|
|
|
* - `/:prefix/:plugin/:controller/:action/*`
|
|
|
|
* - `/:prefix/:controller`
|
|
|
|
* - `/:prefix/:controller/:action/*`
|
|
|
|
* - `/:plugin/:controller`
|
|
|
|
* - `/:plugin/:controller/:action/*`
|
|
|
|
* - `/:controller'
|
|
|
|
* - `/:controller/:action/*'
|
|
|
|
*
|
|
|
|
* A prefix route is generated for each Routing.prefixes declared in core.php. You can disable the
|
|
|
|
* connection of default routes with Router::defaults().
|
2008-10-12 03:52:24 +00:00
|
|
|
*
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __connectDefaultRoutes() {
|
2009-06-10 23:13:39 +00:00
|
|
|
if ($plugins = App::objects('plugin')) {
|
2008-09-12 05:11:34 +00:00
|
|
|
foreach ($plugins as $key => $value) {
|
|
|
|
$plugins[$key] = Inflector::underscore($value);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$match = array('plugin' => implode('|', $plugins));
|
|
|
|
|
2009-09-27 04:12:03 +00:00
|
|
|
foreach ($this->__prefixes as $prefix) {
|
|
|
|
$params = array('prefix' => $prefix, $prefix => true);
|
2009-11-29 21:09:45 +00:00
|
|
|
$indexParams = $params + array('action' => 'index');
|
|
|
|
$this->connect("/{$prefix}/:plugin/:controller", $indexParams, $match);
|
2009-09-27 04:12:03 +00:00
|
|
|
$this->connect("/{$prefix}/:plugin/:controller/:action/*", $params, $match);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-01-18 15:05:23 +00:00
|
|
|
$this->connect('/:plugin/:controller', array('action' => 'index'), $match);
|
2009-12-01 19:39:23 +00:00
|
|
|
$this->connect('/:plugin/:controller/:action/*', array(), $match);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-09-27 04:12:03 +00:00
|
|
|
foreach ($this->__prefixes as $prefix) {
|
|
|
|
$params = array('prefix' => $prefix, $prefix => true);
|
2009-11-29 21:09:45 +00:00
|
|
|
$indexParams = $params + array('action' => 'index');
|
|
|
|
$this->connect("/{$prefix}/:controller", $indexParams);
|
2009-09-27 04:12:03 +00:00
|
|
|
$this->connect("/{$prefix}/:controller/:action/*", $params);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-10-12 03:52:24 +00:00
|
|
|
$this->connect('/:controller', array('action' => 'index'));
|
|
|
|
$this->connect('/:controller/:action/*');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-10-12 03:52:24 +00:00
|
|
|
if ($this->named['rules'] === false) {
|
|
|
|
$this->connectNamed(true);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-10-12 03:52:24 +00:00
|
|
|
$this->__defaultsMapped = true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 20:37:55 +00:00
|
|
|
* Takes parameter and path information back from the Dispatcher, sets these
|
|
|
|
* parameters as the current request parameters that are merged with url arrays
|
|
|
|
* created later in the request.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param array $params Parameters and path information
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function setRequestInfo($params) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
$defaults = array('plugin' => null, 'controller' => null, 'action' => null);
|
|
|
|
$params[0] = array_merge($defaults, (array)$params[0]);
|
|
|
|
$params[1] = array_merge($defaults, (array)$params[1]);
|
2009-11-26 04:40:52 +00:00
|
|
|
list($self->__params[], $self->__paths[]) = $params;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-11-26 04:40:52 +00:00
|
|
|
if (count($self->__paths)) {
|
|
|
|
if (isset($self->__paths[0]['namedArgs'])) {
|
|
|
|
foreach ($self->__paths[0]['namedArgs'] as $arg => $value) {
|
|
|
|
$self->named['rules'][$arg] = true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets parameter information
|
|
|
|
*
|
2010-01-25 20:37:55 +00:00
|
|
|
* @param boolean $current Get current request parameter, useful when using requestAction
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array Parameter information
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function getParams($current = false) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($current) {
|
2009-11-26 04:40:52 +00:00
|
|
|
return $self->__params[count($self->__params) - 1];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-11-26 04:40:52 +00:00
|
|
|
if (isset($self->__params[0])) {
|
|
|
|
return $self->__params[0];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return array();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets URL parameter by name
|
|
|
|
*
|
|
|
|
* @param string $name Parameter name
|
2010-01-25 20:37:55 +00:00
|
|
|
* @param boolean $current Current parameter, useful when using requestAction
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Parameter value
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function getParam($name = 'controller', $current = false) {
|
|
|
|
$params = Router::getParams($current);
|
|
|
|
if (isset($params[$name])) {
|
|
|
|
return $params[$name];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets path information
|
|
|
|
*
|
2010-01-25 20:37:55 +00:00
|
|
|
* @param boolean $current Current parameter, useful when using requestAction
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function getPaths($current = false) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($current) {
|
2009-11-26 04:40:52 +00:00
|
|
|
return $self->__paths[count($self->__paths) - 1];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-11-26 04:40:52 +00:00
|
|
|
if (!isset($self->__paths[0])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return array('base' => null);
|
|
|
|
}
|
2009-11-26 04:40:52 +00:00
|
|
|
return $self->__paths[0];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 20:37:55 +00:00
|
|
|
* Reloads default Router settings. Resets all class variables and
|
|
|
|
* removes all connected routes.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @access public
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function reload() {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach (get_class_vars('Router') as $key => $val) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self->{$key} = $val;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-11-26 04:40:52 +00:00
|
|
|
$self->__setPrefixes();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Promote a route (by default, the last one added) to the beginning of the list
|
|
|
|
*
|
|
|
|
* @param $which A zero-based array index representing the route to move. For example,
|
2010-01-25 20:37:55 +00:00
|
|
|
* if 3 routes have been added, the last route would be 2.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean Retuns false if no route exists at the position specified by $which.
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function promote($which = null) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
2008-09-14 17:17:49 +00:00
|
|
|
if ($which === null) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$which = count($self->routes) - 1;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-11-26 04:40:52 +00:00
|
|
|
if (!isset($self->routes[$which])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-12-02 03:31:18 +00:00
|
|
|
$route =& $self->routes[$which];
|
2009-11-26 04:40:52 +00:00
|
|
|
unset($self->routes[$which]);
|
|
|
|
array_unshift($self->routes, $route);
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Finds URL for specified action.
|
|
|
|
*
|
|
|
|
* Returns an URL pointing to a combination of controller and action. Param
|
|
|
|
* $url can be:
|
2009-02-17 18:26:37 +00:00
|
|
|
*
|
2009-11-29 14:49:19 +00:00
|
|
|
* - Empty - the method will find address to actuall controller/action.
|
2009-02-04 05:00:59 +00:00
|
|
|
* - '/' - the method will find base URL of application.
|
|
|
|
* - A combination of controller/action - the method will find url for it.
|
|
|
|
*
|
2010-01-28 18:30:34 +00:00
|
|
|
* 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.
|
|
|
|
* - `?` - Takes an array of query string parameters
|
|
|
|
* - `#` - Allows you to set url hash fragments.
|
|
|
|
* - `full_base` - If true the `FULL_BASE_URL` constant will be prepended to generated urls.
|
|
|
|
*
|
2009-02-04 05:00:59 +00:00
|
|
|
* @param mixed $url Cake-relative URL, like "/products/edit/92" or "/presidents/elect/4"
|
|
|
|
* or an array specifying any of the following: 'controller', 'action',
|
|
|
|
* and/or 'plugin', in addition to named arguments (keyed array elements),
|
|
|
|
* and standard URL arguments (indexed array elements)
|
|
|
|
* @param mixed $full If (bool) true, the full base URL will be prepended to the result.
|
|
|
|
* If an array accepts the following keys
|
|
|
|
* - escape - used when making urls embedded in html escapes query string '&'
|
|
|
|
* - full - if true the full base URL will be prepended.
|
|
|
|
* @return string Full translated URL with base path.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function url($url = null, $full = false) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
$defaults = $params = array('plugin' => null, 'controller' => null, 'action' => 'index');
|
2009-02-17 18:26:37 +00:00
|
|
|
|
2009-01-14 21:38:11 +00:00
|
|
|
if (is_bool($full)) {
|
|
|
|
$escape = false;
|
|
|
|
} else {
|
2009-11-29 14:49:19 +00:00
|
|
|
extract($full + array('escape' => false, 'full' => false));
|
2009-01-14 21:38:11 +00:00
|
|
|
}
|
2009-02-17 18:26:37 +00:00
|
|
|
|
2009-11-26 04:40:52 +00:00
|
|
|
if (!empty($self->__params)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($this) && !isset($this->params['requested'])) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$params = $self->__params[0];
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2009-11-26 04:40:52 +00:00
|
|
|
$params = end($self->__params);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-09-05 05:06:18 +00:00
|
|
|
if (isset($params['prefix']) && strpos($params['action'], $params['prefix']) === 0) {
|
|
|
|
$params['action'] = substr($params['action'], strlen($params['prefix']) + 1);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$path = array('base' => null);
|
|
|
|
|
2009-11-26 04:40:52 +00:00
|
|
|
if (!empty($self->__paths)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($this) && !isset($this->params['requested'])) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$path = $self->__paths[0];
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2009-11-26 04:40:52 +00:00
|
|
|
$path = end($self->__paths);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$base = $path['base'];
|
|
|
|
$extension = $output = $mapped = $q = $frag = null;
|
|
|
|
|
|
|
|
if (is_array($url)) {
|
2008-06-11 08:54:27 +00:00
|
|
|
if (isset($url['base']) && $url['base'] === false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$base = null;
|
|
|
|
unset($url['base']);
|
|
|
|
}
|
2008-09-14 17:17:49 +00:00
|
|
|
if (isset($url['full_base']) && $url['full_base'] === true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$full = true;
|
|
|
|
unset($url['full_base']);
|
|
|
|
}
|
|
|
|
if (isset($url['?'])) {
|
|
|
|
$q = $url['?'];
|
|
|
|
unset($url['?']);
|
|
|
|
}
|
|
|
|
if (isset($url['#'])) {
|
|
|
|
$frag = '#' . urlencode($url['#']);
|
|
|
|
unset($url['#']);
|
|
|
|
}
|
|
|
|
if (empty($url['action'])) {
|
2008-09-14 17:17:49 +00:00
|
|
|
if (empty($url['controller']) || $params['controller'] === $url['controller']) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$url['action'] = $params['action'];
|
|
|
|
} else {
|
|
|
|
$url['action'] = 'index';
|
|
|
|
}
|
|
|
|
}
|
2009-09-27 04:12:03 +00:00
|
|
|
|
2009-11-26 04:40:52 +00:00
|
|
|
$prefixExists = (array_intersect_key($url, array_flip($self->__prefixes)));
|
|
|
|
foreach ($self->__prefixes as $prefix) {
|
2010-01-27 20:38:18 +00:00
|
|
|
if (!empty($params[$prefix]) && !$prefixExists) {
|
2009-09-27 04:12:03 +00:00
|
|
|
$url[$prefix] = true;
|
|
|
|
} elseif (isset($url[$prefix]) && !$url[$prefix]) {
|
|
|
|
unset($url[$prefix]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-30 15:15:39 +00:00
|
|
|
|
2009-12-02 02:37:39 +00:00
|
|
|
$url += array('controller' => $params['controller'], 'plugin' => $params['plugin']);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (isset($url['ext'])) {
|
|
|
|
$extension = '.' . $url['ext'];
|
|
|
|
unset($url['ext']);
|
|
|
|
}
|
|
|
|
$match = false;
|
2009-10-08 04:35:06 +00:00
|
|
|
|
2009-11-26 04:40:52 +00:00
|
|
|
for ($i = 0, $len = count($self->routes); $i < $len; $i++) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$originalUrl = $url;
|
2008-08-29 18:17:14 +00:00
|
|
|
|
2009-12-02 05:20:51 +00:00
|
|
|
if (isset($self->routes[$i]->options['persist'], $params)) {
|
2009-12-02 02:56:03 +00:00
|
|
|
$url = $self->routes[$i]->persistParams($url, $params);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-11-30 15:15:39 +00:00
|
|
|
|
2009-12-02 02:56:03 +00:00
|
|
|
if ($match = $self->routes[$i]->match($url)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$output = trim($match, '/');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$url = $originalUrl;
|
|
|
|
}
|
|
|
|
if ($match === false) {
|
2009-12-02 03:21:33 +00:00
|
|
|
$output = $self->_handleNoRoute($url);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$output = str_replace('//', '/', $base . '/' . $output);
|
|
|
|
} else {
|
2008-09-12 05:11:34 +00:00
|
|
|
if (((strpos($url, '://')) || (strpos($url, 'javascript:') === 0) || (strpos($url, 'mailto:') === 0)) || (!strncmp($url, '#', 1))) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
if (empty($url)) {
|
|
|
|
if (!isset($path['here'])) {
|
|
|
|
$path['here'] = '/';
|
|
|
|
}
|
2008-06-14 21:12:56 +00:00
|
|
|
$output = $path['here'];
|
2008-09-14 17:17:49 +00:00
|
|
|
} elseif (substr($url, 0, 1) === '/') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$output = $base . $url;
|
|
|
|
} else {
|
|
|
|
$output = $base . '/';
|
2009-11-26 04:40:52 +00:00
|
|
|
foreach ($self->__prefixes as $prefix) {
|
2009-09-27 04:12:03 +00:00
|
|
|
if (isset($params[$prefix])) {
|
|
|
|
$output .= $prefix . '/';
|
2009-09-30 14:01:08 +00:00
|
|
|
break;
|
2009-09-27 04:12:03 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (!empty($params['plugin']) && $params['plugin'] !== $params['controller']) {
|
|
|
|
$output .= Inflector::underscore($params['plugin']) . '/';
|
|
|
|
}
|
|
|
|
$output .= Inflector::underscore($params['controller']) . '/' . $url;
|
|
|
|
}
|
|
|
|
$output = str_replace('//', '/', $output);
|
|
|
|
}
|
2008-10-30 15:05:46 +00:00
|
|
|
if ($full && defined('FULL_BASE_URL')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$output = FULL_BASE_URL . $output;
|
|
|
|
}
|
2008-09-14 17:17:49 +00:00
|
|
|
if (!empty($extension) && substr($output, -1) === '/') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$output = substr($output, 0, -1);
|
|
|
|
}
|
2009-12-02 03:31:18 +00:00
|
|
|
|
2009-11-26 04:40:52 +00:00
|
|
|
return $output . $extension . $self->queryString($q, array(), $escape) . $frag;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-12-02 02:56:03 +00:00
|
|
|
/**
|
|
|
|
* A special fallback method that handles url arrays that cannot match
|
|
|
|
* any defined routes.
|
|
|
|
*
|
|
|
|
* @param array $url A url that didn't match any routes
|
|
|
|
* @return string A generated url for the array
|
2010-01-26 19:10:58 +00:00
|
|
|
* @access protected
|
2009-12-02 02:56:03 +00:00
|
|
|
* @see Router::url()
|
|
|
|
*/
|
2009-12-02 03:21:33 +00:00
|
|
|
function _handleNoRoute($url) {
|
2009-12-02 02:56:03 +00:00
|
|
|
$named = $args = array();
|
|
|
|
$skip = array_merge(
|
|
|
|
array('bare', 'action', 'controller', 'plugin', 'prefix'),
|
|
|
|
$this->__prefixes
|
|
|
|
);
|
|
|
|
|
|
|
|
$keys = array_values(array_diff(array_keys($url), $skip));
|
|
|
|
$count = count($keys);
|
|
|
|
|
|
|
|
// Remove this once parsed URL parameters can be inserted into 'pass'
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
if (is_numeric($keys[$i])) {
|
|
|
|
$args[] = $url[$keys[$i]];
|
|
|
|
} else {
|
|
|
|
$named[$keys[$i]] = $url[$keys[$i]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list($args, $named) = array(Set::filter($args, true), Set::filter($named, true));
|
|
|
|
foreach ($this->__prefixes as $prefix) {
|
|
|
|
if (!empty($url[$prefix])) {
|
|
|
|
$url['action'] = str_replace($prefix . '_', '', $url['action']);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($named) && empty($args) && (!isset($url['action']) || $url['action'] === 'index')) {
|
|
|
|
$url['action'] = null;
|
|
|
|
}
|
|
|
|
|
2009-12-02 03:21:33 +00:00
|
|
|
$urlOut = array_filter(array($url['controller'], $url['action']));
|
2009-12-02 02:56:03 +00:00
|
|
|
|
|
|
|
if (isset($url['plugin']) && $url['plugin'] != $url['controller']) {
|
|
|
|
array_unshift($urlOut, $url['plugin']);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->__prefixes as $prefix) {
|
|
|
|
if (isset($url[$prefix])) {
|
|
|
|
array_unshift($urlOut, $prefix);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$output = implode('/', $urlOut);
|
|
|
|
|
|
|
|
if (!empty($args)) {
|
2009-12-02 03:21:33 +00:00
|
|
|
$output .= '/' . implode('/', $args);
|
2009-12-02 02:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($named)) {
|
|
|
|
foreach ($named as $name => $value) {
|
|
|
|
$output .= '/' . $name . $this->named['separator'] . $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Takes an array of URL parameters and separates the ones that can be used as named arguments
|
|
|
|
*
|
2009-11-29 04:12:44 +00:00
|
|
|
* @param array $params Associative array of URL parameters.
|
|
|
|
* @param string $controller Name of controller being routed. Used in scoping.
|
|
|
|
* @param string $action Name of action being routed. Used in scoping.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function getNamedElements($params, $controller = null, $action = null) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
$named = array();
|
|
|
|
|
|
|
|
foreach ($params as $param => $val) {
|
2009-11-26 04:40:52 +00:00
|
|
|
if (isset($self->named['rules'][$param])) {
|
|
|
|
$rule = $self->named['rules'][$param];
|
2008-05-30 11:40:08 +00:00
|
|
|
if (Router::matchNamed($param, $val, $rule, compact('controller', 'action'))) {
|
|
|
|
$named[$param] = $val;
|
|
|
|
unset($params[$param]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array($named, $params);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Return true if a given named $param's $val matches a given $rule depending on $context. Currently implemented
|
|
|
|
* rule types are controller, action and match that can be combined with each other.
|
|
|
|
*
|
|
|
|
* @param string $param The name of the named parameter
|
|
|
|
* @param string $val The value of the named parameter
|
|
|
|
* @param array $rule The rule(s) to apply, can also be a match string
|
|
|
|
* @param string $context An array with additional context information (controller / action)
|
|
|
|
* @return boolean
|
|
|
|
* @access public
|
2010-01-26 19:10:58 +00:00
|
|
|
* @static
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function matchNamed($param, $val, $rule, $context = array()) {
|
|
|
|
if ($rule === true || $rule === false) {
|
|
|
|
return $rule;
|
|
|
|
}
|
|
|
|
if (is_string($rule)) {
|
|
|
|
$rule = array('match' => $rule);
|
|
|
|
}
|
|
|
|
if (!is_array($rule)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$controllerMatches = !isset($rule['controller'], $context['controller']) || in_array($context['controller'], (array)$rule['controller']);
|
|
|
|
if (!$controllerMatches) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$actionMatches = !isset($rule['action'], $context['action']) || in_array($context['action'], (array)$rule['action']);
|
|
|
|
if (!$actionMatches) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-29 14:49:19 +00:00
|
|
|
return (!isset($rule['match']) || preg_match('/' . $rule['match'] . '/', $val));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Generates a well-formed querystring from $q
|
|
|
|
*
|
|
|
|
* @param mixed $q Query string
|
2009-01-14 21:38:11 +00:00
|
|
|
* @param array $extra Extra querystring parameters.
|
|
|
|
* @param bool $escape Whether or not to use escaped &
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
2009-01-14 21:38:11 +00:00
|
|
|
function queryString($q, $extra = array(), $escape = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($q) && empty($extra)) {
|
|
|
|
return null;
|
|
|
|
}
|
2009-01-14 21:38:11 +00:00
|
|
|
$join = '&';
|
|
|
|
if ($escape === true) {
|
|
|
|
$join = '&';
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$out = '';
|
|
|
|
|
|
|
|
if (is_array($q)) {
|
|
|
|
$q = array_merge($extra, $q);
|
|
|
|
} else {
|
|
|
|
$out = $q;
|
|
|
|
$q = $extra;
|
|
|
|
}
|
2009-01-14 21:38:11 +00:00
|
|
|
$out .= http_build_query($q, null, $join);
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($out[0]) && $out[0] != '?') {
|
|
|
|
$out = '?' . $out;
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-12-28 04:09:26 +00:00
|
|
|
/**
|
|
|
|
* Reverses a parsed parameter array into a string. Works similarily to Router::url(), but
|
|
|
|
* Since parsed URL's contain additional 'pass' and 'named' as well as 'url.url' keys.
|
|
|
|
* Those keys need to be specially handled in order to reverse a params array into a string url.
|
|
|
|
*
|
|
|
|
* @param array $param The params array that needs to be reversed.
|
|
|
|
* @return string The string that is the reversed result of the array
|
|
|
|
* @access public
|
2010-01-26 19:10:58 +00:00
|
|
|
* @static
|
2009-12-28 04:09:26 +00:00
|
|
|
*/
|
|
|
|
function reverse($params) {
|
|
|
|
$pass = $params['pass'];
|
|
|
|
$named = $params['named'];
|
|
|
|
$url = $params['url'];
|
2009-12-28 18:34:05 +00:00
|
|
|
unset($params['pass'], $params['named'], $params['paging'], $params['models'], $params['url'], $url['url']);
|
2009-12-28 04:09:26 +00:00
|
|
|
$params = array_merge($params, $pass, $named);
|
|
|
|
if (!empty($url)) {
|
2009-12-28 04:10:29 +00:00
|
|
|
$params['?'] = $url;
|
2009-12-28 04:09:26 +00:00
|
|
|
}
|
|
|
|
return Router::url($params);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-03-20 00:44:18 +00:00
|
|
|
* Normalizes a URL for purposes of comparison. Will strip the base path off
|
|
|
|
* and replace any double /'s. It will not unify the casing and underscoring
|
|
|
|
* of the input value.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-03-20 00:44:18 +00:00
|
|
|
* @param mixed $url URL to normalize Either an array or a string url.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Normalized URL
|
|
|
|
* @access public
|
2009-11-29 04:12:44 +00:00
|
|
|
* @static
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function normalize($url = '/') {
|
|
|
|
if (is_array($url)) {
|
|
|
|
$url = Router::url($url);
|
2008-07-25 11:45:00 +00:00
|
|
|
} elseif (preg_match('/^[a-z\-]+:\/\//', $url)) {
|
|
|
|
return $url;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$paths = Router::getPaths();
|
|
|
|
|
|
|
|
if (!empty($paths['base']) && stristr($url, $paths['base'])) {
|
2009-07-20 13:18:38 +00:00
|
|
|
$url = preg_replace('/^' . preg_quote($paths['base'], '/') . '/', '', $url, 1);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$url = '/' . $url;
|
|
|
|
|
|
|
|
while (strpos($url, '//') !== false) {
|
|
|
|
$url = str_replace('//', '/', $url);
|
|
|
|
}
|
2008-09-12 05:11:34 +00:00
|
|
|
$url = preg_replace('/(?:(\/$))/', '', $url);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (empty($url)) {
|
|
|
|
return '/';
|
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the route matching the current request URL.
|
|
|
|
*
|
2010-01-25 20:37:55 +00:00
|
|
|
* @return CakeRoute Matching route object.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
2010-01-15 04:38:39 +00:00
|
|
|
function &requestRoute() {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
|
|
|
return $self->__currentRoute[0];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the route matching the current request (useful for requestAction traces)
|
|
|
|
*
|
2010-01-25 20:37:55 +00:00
|
|
|
* @return CakeRoute Matching route object.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
2010-01-15 04:38:39 +00:00
|
|
|
function ¤tRoute() {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
|
|
|
return $self->__currentRoute[count($self->__currentRoute) - 1];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Removes the plugin name from the base URL.
|
|
|
|
*
|
|
|
|
* @param string $base Base URL
|
|
|
|
* @param string $plugin Plugin name
|
|
|
|
* @return base url with plugin name removed if present
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
2009-07-22 13:02:31 +00:00
|
|
|
function stripPlugin($base, $plugin = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($plugin != null) {
|
2008-09-12 05:11:34 +00:00
|
|
|
$base = preg_replace('/(?:' . $plugin . ')/', '', $base);
|
2008-05-30 11:40:08 +00:00
|
|
|
$base = str_replace('//', '', $base);
|
|
|
|
$pos1 = strrpos($base, '/');
|
|
|
|
$char = strlen($base) - 1;
|
|
|
|
|
2008-09-14 17:17:49 +00:00
|
|
|
if ($pos1 === $char) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$base = substr($base, 0, $char);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $base;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Instructs the router to parse out file extensions from the URL. For example,
|
|
|
|
* http://example.com/posts.rss would yield an file extension of "rss".
|
|
|
|
* The file extension itself is made available in the controller as
|
|
|
|
* $this->params['url']['ext'], and is used by the RequestHandler component to
|
|
|
|
* automatically switch to alternate layouts and templates, and load helpers
|
|
|
|
* corresponding to the given content, i.e. RssHelper.
|
|
|
|
*
|
|
|
|
* A list of valid extension can be passed to this method, i.e. Router::parseExtensions('rss', 'xml');
|
|
|
|
* If no parameters are given, anything after the first . (dot) after the last / in the URL will be
|
|
|
|
* parsed, excluding querystring parameters (i.e. ?q=...).
|
|
|
|
*
|
|
|
|
* @access public
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function parseExtensions() {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
|
|
|
$self->__parseExtensions = true;
|
2008-05-30 11:40:08 +00:00
|
|
|
if (func_num_args() > 0) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self->__validExtensions = func_get_args();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Takes an passed params and converts it to args
|
|
|
|
*
|
|
|
|
* @param array $params
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return array Array containing passed and named parameters
|
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function getArgs($args, $options = array()) {
|
2009-11-26 04:40:52 +00:00
|
|
|
$self =& Router::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
$pass = $named = array();
|
|
|
|
$args = explode('/', $args);
|
|
|
|
|
2009-11-29 14:49:19 +00:00
|
|
|
$greedy = isset($options['greedy']) ? $options['greedy'] : $self->named['greedy'];
|
2008-05-30 11:40:08 +00:00
|
|
|
$context = array();
|
|
|
|
if (isset($options['context'])) {
|
|
|
|
$context = $options['context'];
|
|
|
|
}
|
2009-11-26 04:40:52 +00:00
|
|
|
$rules = $self->named['rules'];
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($options['named'])) {
|
2008-09-14 17:17:49 +00:00
|
|
|
$greedy = isset($options['greedy']) && $options['greedy'] === true;
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ((array)$options['named'] as $key => $val) {
|
|
|
|
if (is_numeric($key)) {
|
|
|
|
$rules[$val] = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$rules[$key] = $val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($args as $param) {
|
|
|
|
if (empty($param) && $param !== '0' && $param !== 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-07-28 09:49:27 +00:00
|
|
|
|
2009-11-26 04:40:52 +00:00
|
|
|
$separatorIsPresent = strpos($param, $self->named['separator']) !== false;
|
2009-07-28 09:49:27 +00:00
|
|
|
if ((!isset($options['named']) || !empty($options['named'])) && $separatorIsPresent) {
|
2009-11-26 04:40:52 +00:00
|
|
|
list($key, $val) = explode($self->named['separator'], $param, 2);
|
2008-05-30 11:40:08 +00:00
|
|
|
$hasRule = isset($rules[$key]);
|
2009-11-26 04:40:52 +00:00
|
|
|
$passIt = (!$hasRule && !$greedy) || ($hasRule && !$self->matchNamed($key, $val, $rules[$key], $context));
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($passIt) {
|
|
|
|
$pass[] = $param;
|
|
|
|
} else {
|
|
|
|
$named[$key] = $val;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$pass[] = $param;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return compact('pass', 'named');
|
|
|
|
}
|
|
|
|
}
|
2009-09-30 13:49:39 +00:00
|
|
|
|
|
|
|
/**
|
2009-11-05 04:05:25 +00:00
|
|
|
* A single Route used by the Router to connect requests to
|
2009-09-30 13:49:39 +00:00
|
|
|
* parameter maps.
|
2009-11-05 04:05:25 +00:00
|
|
|
*
|
2009-09-30 13:49:39 +00:00
|
|
|
* Not normally created as a standalone. Use Router::connect() to create
|
|
|
|
* Routes for your application.
|
|
|
|
*
|
|
|
|
* @package cake.libs
|
|
|
|
* @since 1.3.0
|
2010-01-26 19:10:58 +00:00
|
|
|
* @see Router::connect()
|
2009-09-30 13:49:39 +00:00
|
|
|
*/
|
2009-12-04 02:20:08 +00:00
|
|
|
class CakeRoute {
|
2009-12-10 02:04:11 +00:00
|
|
|
|
2009-09-30 13:49:39 +00:00
|
|
|
/**
|
|
|
|
* An array of named segments in a Route.
|
2010-01-25 20:37:55 +00:00
|
|
|
* `/:controller/:action/:id` has 3 key elements
|
2009-09-30 13:49:39 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2009-12-10 02:04:11 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $keys = array();
|
2009-12-10 02:04:11 +00:00
|
|
|
|
2009-09-30 13:49:39 +00:00
|
|
|
/**
|
|
|
|
* An array of additional parameters for the Route.
|
|
|
|
*
|
|
|
|
* @var array
|
2009-12-10 02:04:11 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $options = array();
|
2010-01-25 20:37:55 +00:00
|
|
|
|
2009-09-30 13:49:39 +00:00
|
|
|
/**
|
|
|
|
* Default parameters for a Route
|
|
|
|
*
|
|
|
|
* @var array
|
2009-12-10 02:04:11 +00:00
|
|
|
* @access public
|
2009-09-30 13:49:39 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $defaults = array();
|
2009-12-10 02:04:11 +00:00
|
|
|
|
2009-09-30 13:49:39 +00:00
|
|
|
/**
|
2009-10-02 05:09:38 +00:00
|
|
|
* The routes template string.
|
|
|
|
*
|
|
|
|
* @var string
|
2009-12-10 02:04:11 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $template = null;
|
2009-12-10 02:04:11 +00:00
|
|
|
|
2009-10-02 05:09:38 +00:00
|
|
|
/**
|
|
|
|
* Is this route a greedy route? Greedy routes have a `/*` in their
|
|
|
|
* template
|
2009-09-30 13:49:39 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2009-12-10 02:04:11 +00:00
|
|
|
* @access protected
|
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_greedy = false;
|
2009-12-10 02:04:11 +00:00
|
|
|
|
2009-09-30 13:49:39 +00:00
|
|
|
/**
|
|
|
|
* The compiled route regular expresssion
|
|
|
|
*
|
|
|
|
* @var string
|
2009-12-10 02:04:11 +00:00
|
|
|
* @access protected
|
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_compiledRoute = null;
|
2009-12-10 02:04:11 +00:00
|
|
|
|
2009-09-30 13:49:39 +00:00
|
|
|
/**
|
|
|
|
* HTTP header shortcut map. Used for evaluating header-based route expressions.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__headerMap = array(
|
2009-10-02 05:36:27 +00:00
|
|
|
'type' => 'content_type',
|
|
|
|
'method' => 'request_method',
|
|
|
|
'server' => 'server_name'
|
2009-09-30 13:49:39 +00:00
|
|
|
);
|
2009-12-10 02:04:11 +00:00
|
|
|
|
2009-09-30 13:49:39 +00:00
|
|
|
/**
|
|
|
|
* Constructor for a Route
|
|
|
|
*
|
2009-10-02 05:09:38 +00:00
|
|
|
* @param string $template Template string with parameter placeholders
|
2009-09-30 13:49:39 +00:00
|
|
|
* @param array $defaults Array of defaults for the route.
|
|
|
|
* @param string $params Array of parameters and additional options for the Route
|
|
|
|
* @return void
|
2009-12-10 02:04:11 +00:00
|
|
|
* @access public
|
2009-09-30 13:49:39 +00:00
|
|
|
*/
|
2009-12-04 02:20:08 +00:00
|
|
|
function CakeRoute($template, $defaults = array(), $options = array()) {
|
2009-10-02 05:09:38 +00:00
|
|
|
$this->template = $template;
|
2009-09-30 13:49:39 +00:00
|
|
|
$this->defaults = (array)$defaults;
|
2009-12-02 05:20:51 +00:00
|
|
|
$this->options = (array)$options;
|
2009-09-30 13:49:39 +00:00
|
|
|
}
|
2009-12-10 02:04:11 +00:00
|
|
|
|
2009-09-30 13:49:39 +00:00
|
|
|
/**
|
|
|
|
* Check if a Route has been compiled into a regular expression.
|
|
|
|
*
|
|
|
|
* @return boolean
|
2009-12-10 02:04:11 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-09-30 13:49:39 +00:00
|
|
|
function compiled() {
|
|
|
|
return !empty($this->_compiledRoute);
|
|
|
|
}
|
2009-12-10 02:04:11 +00:00
|
|
|
|
2009-09-30 13:49:39 +00:00
|
|
|
/**
|
2010-01-25 20:37:55 +00:00
|
|
|
* Compiles the route's regular expression. Modifies defaults property so all necessary keys are set
|
2009-09-30 13:49:39 +00:00
|
|
|
* and populates $this->names with the named routing elements.
|
|
|
|
*
|
|
|
|
* @return array Returns a string regular expression of the compiled route.
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function compile() {
|
2009-10-02 05:36:27 +00:00
|
|
|
if ($this->compiled()) {
|
|
|
|
return $this->_compiledRoute;
|
|
|
|
}
|
2009-12-02 05:26:43 +00:00
|
|
|
$this->_writeRoute();
|
2009-09-30 13:49:39 +00:00
|
|
|
return $this->_compiledRoute;
|
|
|
|
}
|
2009-12-10 02:04:11 +00:00
|
|
|
|
2009-09-30 13:49:39 +00:00
|
|
|
/**
|
2009-12-02 05:26:43 +00:00
|
|
|
* Builds a route regular expression. Uses the template, defaults and options
|
2010-01-25 20:37:55 +00:00
|
|
|
* properties to compile a regular expression that can be used to parse request strings.
|
2009-09-30 13:49:39 +00:00
|
|
|
*
|
2009-10-08 18:04:14 +00:00
|
|
|
* @return void
|
2009-09-30 13:49:39 +00:00
|
|
|
* @access protected
|
|
|
|
*/
|
2009-12-02 05:26:43 +00:00
|
|
|
function _writeRoute() {
|
|
|
|
if (empty($this->template) || ($this->template === '/')) {
|
2009-10-08 18:04:14 +00:00
|
|
|
$this->_compiledRoute = '#^/*$#';
|
2009-10-02 05:09:38 +00:00
|
|
|
$this->keys = array();
|
2009-10-08 18:04:14 +00:00
|
|
|
return;
|
2009-09-30 13:49:39 +00:00
|
|
|
}
|
2009-12-02 05:26:43 +00:00
|
|
|
$route = $this->template;
|
2009-11-27 02:42:33 +00:00
|
|
|
$names = $replacements = $search = array();
|
2009-12-02 05:26:43 +00:00
|
|
|
$parsed = preg_quote($this->template, '#');
|
2009-11-26 05:17:43 +00:00
|
|
|
|
|
|
|
preg_match_all('#:([A-Za-z0-9_-]+[A-Z0-9a-z])#', $route, $namedElements);
|
|
|
|
foreach ($namedElements[1] as $i => $name) {
|
2009-12-02 05:26:43 +00:00
|
|
|
if (isset($this->options[$name])) {
|
2009-11-28 14:40:15 +00:00
|
|
|
$option = null;
|
2009-12-02 05:26:43 +00:00
|
|
|
if ($name !== 'plugin' && array_key_exists($name, $this->defaults)) {
|
2009-11-26 05:17:43 +00:00
|
|
|
$option = '?';
|
|
|
|
}
|
2009-11-27 14:58:51 +00:00
|
|
|
$slashParam = '/\\' . $namedElements[0][$i];
|
|
|
|
if (strpos($parsed, $slashParam) !== false) {
|
2009-12-02 05:26:43 +00:00
|
|
|
$replacements[] = '(?:/(?P<' . $name . '>' . $this->options[$name] . ')' . $option . ')' . $option;
|
2009-11-27 14:58:51 +00:00
|
|
|
$search[] = $slashParam;
|
|
|
|
} else {
|
|
|
|
$search[] = '\\' . $namedElements[0][$i];
|
2009-12-02 05:26:43 +00:00
|
|
|
$replacements[] = '(?:(?P<' . $name . '>' . $this->options[$name] . ')' . $option . ')' . $option;
|
2009-11-27 14:58:51 +00:00
|
|
|
}
|
2009-11-26 05:17:43 +00:00
|
|
|
} else {
|
2010-01-25 17:49:12 +00:00
|
|
|
$replacements[] = '(?:(?P<' . $name . '>[^/]+))';
|
2009-11-27 14:58:51 +00:00
|
|
|
$search[] = '\\' . $namedElements[0][$i];
|
2009-11-26 05:17:43 +00:00
|
|
|
}
|
|
|
|
$names[] = $name;
|
|
|
|
}
|
2009-11-29 02:54:33 +00:00
|
|
|
if (preg_match('#\/\*$#', $route, $m)) {
|
|
|
|
$parsed = preg_replace('#/\\\\\*$#', '(?:/(?P<_args_>.*))?', $parsed);
|
2009-11-29 21:09:45 +00:00
|
|
|
$this->_greedy = true;
|
2009-11-26 14:41:05 +00:00
|
|
|
}
|
2009-11-27 14:58:51 +00:00
|
|
|
$parsed = str_replace($search, $replacements, $parsed);
|
2009-11-27 02:42:33 +00:00
|
|
|
$this->_compiledRoute = '#^' . $parsed . '[/]*$#';
|
2009-11-26 05:17:43 +00:00
|
|
|
$this->keys = $names;
|
2009-09-30 13:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-10-02 05:09:38 +00:00
|
|
|
* 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
|
2010-01-25 20:37:55 +00:00
|
|
|
* false will be returned. String urls are parsed if they match a routes regular expression.
|
2009-09-30 13:49:39 +00:00
|
|
|
*
|
2009-10-02 05:09:38 +00:00
|
|
|
* @param string $url The url to attempt to parse.
|
|
|
|
* @return mixed Boolean false on failure, otherwise an array or parameters
|
2009-12-10 02:04:11 +00:00
|
|
|
* @access public
|
2009-09-30 13:49:39 +00:00
|
|
|
*/
|
2009-10-02 05:09:38 +00:00
|
|
|
function parse($url) {
|
2009-09-30 13:49:39 +00:00
|
|
|
if (!$this->compiled()) {
|
|
|
|
$this->compile();
|
|
|
|
}
|
2009-11-28 14:40:15 +00:00
|
|
|
if (!preg_match($this->_compiledRoute, $url, $route)) {
|
2009-09-30 13:49:39 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
foreach ($this->defaults as $key => $val) {
|
2009-11-28 14:40:15 +00:00
|
|
|
if ($key[0] === '[' && preg_match('/^\[(\w+)\]$/', $key, $header)) {
|
2009-09-30 13:49:39 +00:00
|
|
|
if (isset($this->__headerMap[$header[1]])) {
|
|
|
|
$header = $this->__headerMap[$header[1]];
|
|
|
|
} else {
|
|
|
|
$header = 'http_' . $header[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
$val = (array)$val;
|
|
|
|
$h = false;
|
|
|
|
|
|
|
|
foreach ($val as $v) {
|
|
|
|
if (env(strtoupper($header)) === $v) {
|
|
|
|
$h = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$h) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-28 14:40:15 +00:00
|
|
|
array_shift($route);
|
|
|
|
$count = count($this->keys);
|
|
|
|
for ($i = 0; $i <= $count; $i++) {
|
|
|
|
unset($route[$i]);
|
|
|
|
}
|
|
|
|
$route['pass'] = $route['named'] = array();
|
|
|
|
$route += $this->defaults;
|
2009-11-29 21:09:45 +00:00
|
|
|
|
2009-11-28 14:40:15 +00:00
|
|
|
foreach ($route as $key => $value) {
|
|
|
|
if (is_integer($key)) {
|
|
|
|
$route['pass'][] = $value;
|
|
|
|
unset($route[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $route;
|
2009-09-30 13:49:39 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-02 05:09:38 +00:00
|
|
|
|
2009-11-29 15:59:38 +00:00
|
|
|
/**
|
2010-01-25 20:37:55 +00:00
|
|
|
* Apply persistent parameters to a url array. Persistant parameters are a special
|
|
|
|
* key used during route creation to force route parameters to persist when omitted from
|
|
|
|
* a url array.
|
2009-11-29 15:59:38 +00:00
|
|
|
*
|
|
|
|
* @param array $url The array to apply persistent parameters to.
|
|
|
|
* @param array $params An array of persistent values to replace persistent ones.
|
|
|
|
* @return array An array with persistent parameters applied.
|
2009-12-10 02:04:11 +00:00
|
|
|
* @access public
|
2009-11-29 15:59:38 +00:00
|
|
|
*/
|
|
|
|
function persistParams($url, $params) {
|
2009-12-02 05:20:51 +00:00
|
|
|
foreach ($this->options['persist'] as $persistKey) {
|
2009-12-14 23:43:29 +00:00
|
|
|
if (array_key_exists($persistKey, $params) && !isset($url[$persistKey])) {
|
2009-11-29 15:59:38 +00:00
|
|
|
$url[$persistKey] = $params[$persistKey];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2009-10-02 05:09:38 +00:00
|
|
|
/**
|
2010-01-25 20:37:55 +00:00
|
|
|
* Attempt to match a url array. If the url matches the route parameters + settings, then
|
|
|
|
* return a generated string url. If the url doesn't match the route parameters false will be returned.
|
|
|
|
* This method handles the reverse routing or conversion of url arrays into string urls.
|
2009-10-02 05:09:38 +00:00
|
|
|
*
|
|
|
|
* @param array $url An array of parameters to check matching with.
|
|
|
|
* @return mixed Either a string url for the parameters if they match or false.
|
2009-12-10 02:04:11 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-10-02 05:09:38 +00:00
|
|
|
function match($url) {
|
|
|
|
if (!$this->compiled()) {
|
|
|
|
$this->compile();
|
|
|
|
}
|
2009-11-30 05:23:41 +00:00
|
|
|
$defaults = $this->defaults;
|
|
|
|
|
|
|
|
if (isset($defaults['prefix'])) {
|
2009-12-02 02:37:39 +00:00
|
|
|
$url['prefix'] = $defaults['prefix'];
|
2009-11-30 05:23:41 +00:00
|
|
|
}
|
|
|
|
|
2009-11-30 03:57:10 +00:00
|
|
|
//check that all the key names are in the url
|
2009-11-29 21:09:45 +00:00
|
|
|
$keyNames = array_flip($this->keys);
|
2009-11-30 03:57:10 +00:00
|
|
|
if (array_intersect_key($keyNames, $url) != $keyNames) {
|
2009-11-29 21:09:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-12-02 04:05:04 +00:00
|
|
|
|
|
|
|
$diffUnfiltered = Set::diff($url, $defaults);
|
|
|
|
$diff = array();
|
2009-12-09 02:36:25 +00:00
|
|
|
|
2009-12-02 04:05:04 +00:00
|
|
|
foreach ($diffUnfiltered as $key => $var) {
|
|
|
|
if ($var === 0 || $var === '0' || !empty($var)) {
|
|
|
|
$diff[$key] = $var;
|
|
|
|
}
|
|
|
|
}
|
2009-11-29 21:09:45 +00:00
|
|
|
|
2009-11-30 03:57:10 +00:00
|
|
|
//if a not a greedy route, no extra params are allowed.
|
2009-11-30 15:15:39 +00:00
|
|
|
if (!$this->_greedy && array_diff_key($diff, $keyNames) != array()) {
|
2009-11-29 21:09:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-01 19:58:36 +00:00
|
|
|
//remove defaults that are also keys. They can cause match failures
|
2009-12-09 02:36:25 +00:00
|
|
|
foreach ($this->keys as $key) {
|
|
|
|
unset($defaults[$key]);
|
2009-12-01 18:37:03 +00:00
|
|
|
}
|
2009-12-01 20:04:53 +00:00
|
|
|
$filteredDefaults = array_filter($defaults);
|
2009-12-01 18:37:03 +00:00
|
|
|
|
2009-12-02 02:37:39 +00:00
|
|
|
//if the difference between the url diff and defaults contains keys from defaults its not a match
|
2009-12-09 02:36:25 +00:00
|
|
|
if (array_intersect_key($filteredDefaults, $diffUnfiltered) !== array()) {
|
2009-11-30 03:57:10 +00:00
|
|
|
return false;
|
2009-11-29 21:09:45 +00:00
|
|
|
}
|
|
|
|
|
2009-12-01 20:04:53 +00:00
|
|
|
$passedArgsAndParams = array_diff_key($diff, $filteredDefaults, $keyNames);
|
2009-11-30 03:57:10 +00:00
|
|
|
list($named, $params) = Router::getNamedElements($passedArgsAndParams, $url['controller'], $url['action']);
|
|
|
|
|
2009-11-30 05:23:41 +00:00
|
|
|
//remove any pass params, they have numeric indexes, skip any params that are in the defaults
|
2009-11-29 21:09:45 +00:00
|
|
|
$pass = array();
|
|
|
|
$i = 0;
|
|
|
|
while (isset($url[$i])) {
|
2009-11-30 05:23:41 +00:00
|
|
|
if (!isset($diff[$i])) {
|
|
|
|
$i++;
|
|
|
|
continue;
|
|
|
|
}
|
2009-11-29 21:09:45 +00:00
|
|
|
$pass[] = $url[$i];
|
2009-12-01 19:39:23 +00:00
|
|
|
unset($url[$i], $params[$i]);
|
2009-11-29 21:09:45 +00:00
|
|
|
$i++;
|
|
|
|
}
|
2009-12-01 19:58:36 +00:00
|
|
|
|
|
|
|
//still some left over parameters that weren't named or passed args, bail.
|
|
|
|
if (!empty($params)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-30 04:46:02 +00:00
|
|
|
//check patterns for routed params
|
2009-12-02 05:20:51 +00:00
|
|
|
if (!empty($this->options)) {
|
|
|
|
foreach ($this->options as $key => $pattern) {
|
2009-12-01 20:04:53 +00:00
|
|
|
if (array_key_exists($key, $url) && !preg_match('#^' . $pattern . '$#', $url[$key])) {
|
2009-11-30 04:46:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-02 02:37:39 +00:00
|
|
|
return $this->_writeUrl(array_merge($url, compact('pass', 'named')));
|
2009-10-02 05:09:38 +00:00
|
|
|
}
|
2009-12-10 02:04:11 +00:00
|
|
|
|
2009-10-02 05:09:38 +00:00
|
|
|
/**
|
2010-01-25 20:37:55 +00:00
|
|
|
* Converts a matching route array into a url string. Composes the string url using the template
|
|
|
|
* used to create the route.
|
2009-10-02 05:09:38 +00:00
|
|
|
*
|
2009-12-10 02:04:11 +00:00
|
|
|
* @param array $params The params to convert to a string url.
|
2010-01-25 20:37:55 +00:00
|
|
|
* @return string Composed route string.
|
2009-11-29 21:09:45 +00:00
|
|
|
* @access protected
|
2009-12-10 02:04:11 +00:00
|
|
|
*/
|
2009-11-29 21:09:45 +00:00
|
|
|
function _writeUrl($params) {
|
2009-11-29 17:10:09 +00:00
|
|
|
if (isset($params['plugin'], $params['controller']) && $params['plugin'] === $params['controller']) {
|
2009-10-02 05:09:38 +00:00
|
|
|
unset($params['controller']);
|
|
|
|
}
|
|
|
|
|
2009-11-29 17:10:09 +00:00
|
|
|
if (isset($params['prefix'], $params['action'])) {
|
2009-10-02 05:09:38 +00:00
|
|
|
$params['action'] = str_replace($params['prefix'] . '_', '', $params['action']);
|
|
|
|
unset($params['prefix']);
|
|
|
|
}
|
|
|
|
|
2009-11-29 17:10:09 +00:00
|
|
|
if (is_array($params['pass'])) {
|
|
|
|
$params['pass'] = implode('/', $params['pass']);
|
2009-10-02 05:09:38 +00:00
|
|
|
}
|
|
|
|
|
2009-11-29 17:10:09 +00:00
|
|
|
$instance =& Router::getInstance();
|
2009-10-02 05:36:27 +00:00
|
|
|
$separator = $instance->named['separator'];
|
|
|
|
|
2010-01-27 20:38:18 +00:00
|
|
|
if (!empty($params['named']) && is_array($params['named'])) {
|
|
|
|
$named = array();
|
|
|
|
foreach ($params['named'] as $key => $value) {
|
|
|
|
$named[] = $key . $separator . $value;
|
2009-10-02 05:09:38 +00:00
|
|
|
}
|
2010-01-27 20:38:18 +00:00
|
|
|
$params['pass'] = $params['pass'] . '/' . implode('/', $named);
|
2009-10-02 05:09:38 +00:00
|
|
|
}
|
|
|
|
$out = $this->template;
|
|
|
|
|
2009-11-29 17:10:09 +00:00
|
|
|
$search = $replace = array();
|
2009-10-02 05:09:38 +00:00
|
|
|
foreach ($this->keys as $key) {
|
|
|
|
$string = null;
|
|
|
|
if (isset($params[$key])) {
|
|
|
|
$string = $params[$key];
|
|
|
|
} elseif (strpos($out, $key) != strlen($out) - strlen($key)) {
|
2010-01-27 20:38:18 +00:00
|
|
|
$key .= '/';
|
2009-10-02 05:09:38 +00:00
|
|
|
}
|
2009-11-29 17:10:09 +00:00
|
|
|
$search[] = ':' . $key;
|
|
|
|
$replace[] = $string;
|
2009-10-02 05:09:38 +00:00
|
|
|
}
|
2009-11-29 17:10:09 +00:00
|
|
|
$out = str_replace($search, $replace, $out);
|
2009-10-02 05:09:38 +00:00
|
|
|
|
|
|
|
if (strpos($this->template, '*')) {
|
|
|
|
$out = str_replace('*', $params['pass'], $out);
|
|
|
|
}
|
2009-11-30 03:57:10 +00:00
|
|
|
$out = str_replace('//', '/', $out);
|
2009-10-02 05:09:38 +00:00
|
|
|
return $out;
|
|
|
|
}
|
2009-09-30 13:49:39 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
?>
|