2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Parses the request URL into controller, action, and parameters.
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, 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.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs
|
2008-10-30 17:30:26 +00:00
|
|
|
* @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
|
|
|
|
2010-12-04 07:04:30 +00:00
|
|
|
App::uses('CakeRequest', 'Network');
|
|
|
|
App::uses('CakeRoute', 'Routing/Route');
|
2010-05-04 04:26:02 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-12-22 03:27:17 +00:00
|
|
|
* Parses the request URL into controller, action, and parameters. Uses the connected routes
|
|
|
|
* to match the incoming url string to parameters that will allow the request to be dispatched. Also
|
|
|
|
* handles converting parameter lists into url strings, using the connected routes. Routing allows you to decouple
|
|
|
|
* the way the world interacts with your application (urls) and the implementation (controllers and actions).
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-12-22 03:27:17 +00:00
|
|
|
* ### Connecting routes
|
|
|
|
*
|
|
|
|
* Connecting routes is done using Router::connect(). When parsing incoming requests or reverse matching
|
|
|
|
* parameters, routes are enumerated in the order they were connected. You can modify the order of connected
|
|
|
|
* routes using Router::promote(). For more information on routes and how to connect them see Router::connect().
|
|
|
|
*
|
|
|
|
* ### Named parameters
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-12-22 03:27:17 +00:00
|
|
|
* Named parameters allow you to embed key:value pairs into path segments. This allows you create hash
|
|
|
|
* structures using urls. You can define how named parameters work in your application using Router::connectNamed()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-12-22 03:27:17 +00:00
|
|
|
* @package 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-05-04 04:26:02 +00:00
|
|
|
public static $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-05-04 04:26:02 +00:00
|
|
|
protected static $_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-05-04 04:26:02 +00:00
|
|
|
protected static $_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-06-29 03:41:48 +00:00
|
|
|
protected static $_validExtensions = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* 'Constant' regular expression definitions for named route elements
|
|
|
|
*
|
|
|
|
*/
|
2010-05-04 04:26:02 +00:00
|
|
|
const ACTION = 'index|show|add|create|edit|update|remove|del|delete|view|item';
|
|
|
|
const YEAR = '[12][0-9]{3}';
|
|
|
|
const MONTH = '0[1-9]|1[012]';
|
|
|
|
const DAY = '0[1-9]|[12][0-9]|3[01]';
|
|
|
|
const ID = '[0-9]+';
|
|
|
|
const 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}';
|
|
|
|
|
2011-03-02 11:20:45 +00:00
|
|
|
private static $__namedExpressions = array(
|
2010-05-04 04:26:02 +00:00
|
|
|
'Action' => Router::ACTION,
|
|
|
|
'Year' => Router::YEAR,
|
|
|
|
'Month' => Router::MONTH,
|
|
|
|
'Day' => Router::DAY,
|
|
|
|
'ID' => Router::ID,
|
|
|
|
'UUID' => Router::UUID
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
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
|
|
|
|
*/
|
2011-03-02 11:20:45 +00:00
|
|
|
protected static $_namedConfig = array(
|
2008-05-30 11:40:08 +00:00
|
|
|
'default' => array('page', 'fields', 'order', 'limit', 'recursive', 'sort', 'direction', 'step'),
|
2011-03-03 12:11:32 +00:00
|
|
|
'greedyNamed' => true,
|
2008-05-30 11:40:08 +00:00
|
|
|
'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-05-04 04:26:02 +00:00
|
|
|
protected static $_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-05-04 04:26:02 +00:00
|
|
|
protected static $_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
|
|
|
|
*/
|
2010-05-04 04:26:02 +00:00
|
|
|
protected static $_resourceMapped = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-05-02 06:16:16 +00:00
|
|
|
* Maintains the request object stack for the current request.
|
|
|
|
* This will contain more than one request object when requestAction is used.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-05-04 04:26:02 +00:00
|
|
|
protected static $_requests = 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-05-04 04:26:02 +00:00
|
|
|
protected static $_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
|
|
|
|
*/
|
2010-05-04 04:26:02 +00:00
|
|
|
protected static $_connectDefaults = true;
|
2009-11-26 04:21:10 +00:00
|
|
|
|
2009-09-27 02:37:19 +00:00
|
|
|
/**
|
2010-05-04 04:26:02 +00:00
|
|
|
* Initial state is popualated the first time reload() is called which is at the bottom
|
|
|
|
* of this file. This is a cheat as get_class_vars() returns the value of static vars even if they
|
|
|
|
* have changed.
|
2009-09-27 02:37:19 +00:00
|
|
|
*
|
2010-05-04 04:26:02 +00:00
|
|
|
* @var array
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-05-04 04:26:02 +00:00
|
|
|
protected static $_initialState = array();
|
2009-09-27 02:37:19 +00:00
|
|
|
|
|
|
|
/**
|
2010-04-21 03:43:51 +00:00
|
|
|
* Sets the Routing prefixes.
|
2009-09-27 02:37:19 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @access private
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-05-04 04:26:02 +00:00
|
|
|
protected static function _setPrefixes() {
|
2009-09-27 02:37:19 +00:00
|
|
|
$routing = Configure::read('Routing');
|
2009-09-30 04:50:15 +00:00
|
|
|
if (!empty($routing['prefixes'])) {
|
2010-05-04 04:26:02 +00:00
|
|
|
self::$_prefixes = array_merge(self::$_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
|
|
|
/**
|
2011-05-23 02:20:56 +00:00
|
|
|
* Gets the named route elements for use in app/Config/routes.php
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return array Named route elements
|
2011-03-02 11:20:45 +00:00
|
|
|
* @see Router::$__namedExpressions
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static function getNamedExpressions() {
|
2011-03-02 11:20:45 +00:00
|
|
|
return self::$__namedExpressions;
|
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.
|
|
|
|
*
|
2011-03-21 01:44:15 +00:00
|
|
|
* $options offers four 'special' keys. `pass`, `named`, `persist` and `routeClass`
|
|
|
|
* have special meaning in the $options array.
|
2010-01-25 20:37:55 +00:00
|
|
|
*
|
|
|
|
* `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
|
2011-01-18 22:23:12 +00:00
|
|
|
* new urls. You can override persistent parameters by redefining them in a url or remove them by
|
2010-01-25 20:37:55 +00:00
|
|
|
* setting the parameter to `false`. Ex. `'persist' => array('lang')`
|
|
|
|
*
|
2011-01-18 22:23:12 +00:00
|
|
|
* `routeClass` is used to extend and change how individual routes parse requests and handle reverse routing,
|
|
|
|
* via a custom routing class. Ex. `'routeClass' => 'SlugRoute'`
|
|
|
|
*
|
2011-03-21 01:44:15 +00:00
|
|
|
* `named` is used to configure named parameters at the route level. This key uses the same options
|
|
|
|
* as Router::connectNamed()
|
|
|
|
*
|
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
|
2011-01-18 22:23:12 +00:00
|
|
|
* shifted into the passed arguments, supplying patterns for routing parameters and supplying the name of a
|
|
|
|
* custom routing class.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see routes
|
2009-09-27 04:12:03 +00:00
|
|
|
* @return array Array of routes
|
2010-12-12 00:01:07 +00:00
|
|
|
* @throws RouterException
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static function connect($route, $defaults = array(), $options = array()) {
|
2010-05-04 04:26:02 +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'])) {
|
2010-05-04 04:26:02 +00:00
|
|
|
self::$_prefixes[] = $defaults['prefix'];
|
|
|
|
self::$_prefixes = array_keys(array_flip(self::$_prefixes));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-10-15 01:54:44 +00:00
|
|
|
$defaults += array('plugin' => null);
|
|
|
|
if (empty($options['action'])) {
|
|
|
|
$defaults += array('action' => 'index');
|
|
|
|
}
|
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'];
|
2010-12-12 00:01:07 +00:00
|
|
|
if (!is_subclass_of($routeClass, 'CakeRoute')) {
|
2011-03-20 15:35:43 +00:00
|
|
|
throw new RouterException(__d('cake_dev', 'Route classes must extend CakeRoute'));
|
2010-12-12 00:01:07 +00:00
|
|
|
}
|
2009-12-04 01:50:13 +00:00
|
|
|
unset($options['routeClass']);
|
2010-12-15 03:58:27 +00:00
|
|
|
if ($routeClass == 'RedirectRoute' && isset($defaults['redirect'])) {
|
|
|
|
$defaults = $defaults['redirect'];
|
|
|
|
}
|
2009-12-04 01:50:13 +00:00
|
|
|
}
|
2010-12-12 00:01:07 +00:00
|
|
|
self::$routes[] = new $routeClass($route, $defaults, $options);
|
2010-05-04 04:26:02 +00:00
|
|
|
return self::$routes;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-11-08 01:33:05 +00:00
|
|
|
/**
|
|
|
|
* Connects a new redirection Route in the router.
|
|
|
|
*
|
|
|
|
* Redirection routes are different from normal routes as they perform an actual
|
|
|
|
* header redirection if a match is found. The redirection can occur within your
|
|
|
|
* application or redirect to an outside location.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
*
|
|
|
|
* `Router::redirect('/home/*', array('controller' => 'posts', 'action' => 'view', array('persist' => true));`
|
|
|
|
*
|
2010-12-18 20:36:22 +00:00
|
|
|
* Redirects /home/* to /posts/view and passes the parameters to /posts/view. Using an array as the
|
2011-03-21 01:44:15 +00:00
|
|
|
* redirect destination allows you to use other routes to define where a url string should be redirected to.
|
2010-11-08 01:33:05 +00:00
|
|
|
*
|
|
|
|
* `Router::redirect('/posts/*', 'http://google.com', array('status' => 302));`
|
|
|
|
*
|
|
|
|
* Redirects /posts/* to http://google.com with a HTTP status of 302
|
|
|
|
*
|
|
|
|
* ### Options:
|
2010-12-18 20:36:22 +00:00
|
|
|
*
|
2010-11-08 01:33:05 +00:00
|
|
|
* - `status` Sets the HTTP status (default 301)
|
2010-12-18 20:36:22 +00:00
|
|
|
* - `persist` Passes the params to the redirected route, if it can. This is useful with greedy routes,
|
|
|
|
* routes that end in `*` are greedy. As you can remap urls and not loose any passed/named args.
|
2010-11-08 01:33:05 +00:00
|
|
|
*
|
|
|
|
* @param string $route A string describing the template of the route
|
|
|
|
* @param array $url A url to redirect to. Can be a string or a Cake array-based url
|
|
|
|
* @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
|
|
|
|
* shifted into the passed arguments. As well as supplying patterns for routing parameters.
|
|
|
|
* @see routes
|
|
|
|
* @return array Array of routes
|
|
|
|
*/
|
2010-12-15 03:58:27 +00:00
|
|
|
public static function redirect($route, $url, $options = array()) {
|
2010-12-11 05:47:55 +00:00
|
|
|
App::uses('RedirectRoute', 'Routing/Route');
|
2010-11-08 01:33:05 +00:00
|
|
|
$options['routeClass'] = 'RedirectRoute';
|
2010-12-15 03:58:27 +00:00
|
|
|
if (is_string($url)) {
|
|
|
|
$url = array('redirect' => $url);
|
|
|
|
}
|
2010-11-08 01:33:05 +00:00
|
|
|
return self::connect($route, $url, $options);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-12-20 01:05:57 +00:00
|
|
|
* Specifies what named parameters CakePHP should be parsing out of incoming urls. By default
|
|
|
|
* CakePHP will parse every named parameter out of incoming URLs. However, if you want to take more
|
|
|
|
* control over how named parameters are parsed you can use one of the following setups:
|
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
|
|
|
*
|
2010-12-20 02:49:17 +00:00
|
|
|
* Parse only the page parameter no matter 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)
|
|
|
|
* );
|
|
|
|
* }}}
|
|
|
|
*
|
2010-12-20 02:49:17 +00:00
|
|
|
* ### Options
|
|
|
|
*
|
|
|
|
* - `greedy` Setting this to true will make Router parse all named params. Setting it to false will
|
|
|
|
* parse only the connected named params.
|
|
|
|
* - `default` Set this to true to merge in the default set of named parameters.
|
|
|
|
* - `reset` Set to true to clear existing rules and start fresh.
|
|
|
|
* - `separator` Change the string used to separate the key & value in a named parameter. Defaults to `:`
|
|
|
|
*
|
2010-01-25 20:37:55 +00:00
|
|
|
* @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
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static function connectNamed($named, $options = array()) {
|
2010-12-20 02:49:17 +00:00
|
|
|
if (isset($options['separator'])) {
|
2011-03-02 11:20:45 +00:00
|
|
|
self::$_namedConfig['separator'] = $options['separator'];
|
2010-12-20 02:49:17 +00:00
|
|
|
unset($options['separator']);
|
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 ($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
|
|
|
|
2011-03-02 11:20:45 +00:00
|
|
|
if ($options['reset'] == true || self::$_namedConfig['rules'] === false) {
|
|
|
|
self::$_namedConfig['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']) {
|
2011-03-02 11:20:45 +00:00
|
|
|
$named = array_merge($named, self::$_namedConfig['default']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($named as $key => $val) {
|
|
|
|
if (is_numeric($key)) {
|
2011-03-02 11:20:45 +00:00
|
|
|
self::$_namedConfig['rules'][$val] = true;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2011-03-02 11:20:45 +00:00
|
|
|
self::$_namedConfig['rules'][$key] = $val;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-03 11:49:11 +00:00
|
|
|
self::$_namedConfig['greedyNamed'] = $options['greedy'];
|
2011-03-02 11:20:45 +00:00
|
|
|
return self::$_namedConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current named parameter configuration values.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @see Router::$_namedConfig
|
|
|
|
*/
|
|
|
|
public static function namedConfig() {
|
|
|
|
return self::$_namedConfig;
|
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-04-20 03:44:45 +00:00
|
|
|
public static function defaults($connect = true) {
|
2010-05-04 04:26:02 +00:00
|
|
|
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
|
2010-04-20 03:44:45 +00:00
|
|
|
* @return array Array of mapped resources
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static function mapResources($controller, $options = array()) {
|
2011-03-02 11:20:45 +00:00
|
|
|
$options = array_merge(array(
|
|
|
|
'prefix' => '/',
|
|
|
|
'id' => self::ID . '|' . self::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
|
|
|
|
2010-05-04 04:26:02 +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'))
|
|
|
|
);
|
|
|
|
}
|
2010-05-04 04:26:02 +00:00
|
|
|
self::$_resourceMapped[] = $urlName;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-04 04:26:02 +00:00
|
|
|
return self::$_resourceMapped;
|
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
|
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static function prefixes() {
|
2010-05-04 04:26:02 +00:00
|
|
|
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-05-02 04:29:35 +00:00
|
|
|
* Parses given URL string. Returns 'routing' parametets for that url.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $url URL to be parsed
|
|
|
|
* @return array Parsed elements from URL
|
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static function parse($url) {
|
2010-05-04 04:26:02 +00:00
|
|
|
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(),
|
2010-05-01 05:43:06 +00:00
|
|
|
'named' => array()
|
2009-12-16 20:04:59 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-04-22 19:52:42 +00:00
|
|
|
$ext = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($url && strpos($url, '/') !== 0) {
|
|
|
|
$url = '/' . $url;
|
|
|
|
}
|
|
|
|
if (strpos($url, '?') !== false) {
|
|
|
|
$url = substr($url, 0, strpos($url, '?'));
|
|
|
|
}
|
2008-08-22 04:06:29 +00:00
|
|
|
|
2010-05-04 04:26:02 +00:00
|
|
|
extract(self::__parseExtension($url));
|
2008-08-22 04:06:29 +00:00
|
|
|
|
2010-05-04 04:26:02 +00:00
|
|
|
for ($i = 0, $len = count(self::$routes); $i < $len; $i++) {
|
|
|
|
$route =& self::$routes[$i];
|
2010-05-01 05:43:06 +00:00
|
|
|
|
2009-10-02 05:36:27 +00:00
|
|
|
if (($r = $route->parse($url)) !== false) {
|
2010-05-04 04:26:02 +00:00
|
|
|
self::$_currentRoute[] =& $route;
|
2009-11-28 14:40:15 +00:00
|
|
|
$out = $r;
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-03-11 03:04:46 +00:00
|
|
|
if (isset($out['prefix'])) {
|
|
|
|
$out['action'] = $out['prefix'] . '_' . $out['action'];
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
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
|
|
|
|
*/
|
2010-11-09 03:08:30 +00:00
|
|
|
private static function __parseExtension($url) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$ext = null;
|
|
|
|
|
2010-05-04 04:26:02 +00:00
|
|
|
if (self::$_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);
|
2010-05-04 04:26:02 +00:00
|
|
|
if (empty(self::$_validExtensions)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$url = substr($url, 0, strpos($url, '.' . $match));
|
|
|
|
$ext = $match;
|
|
|
|
} else {
|
2010-05-04 04:26:02 +00:00
|
|
|
foreach (self::$_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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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:
|
|
|
|
*
|
2010-04-04 04:13:16 +00:00
|
|
|
* For each of the Routing.prefixes the following routes are created. Routes containing `:plugin` are only
|
|
|
|
* created when your application has one or more plugins.
|
|
|
|
*
|
|
|
|
* - `/:prefix/:plugin` a plugin shortcut route.
|
|
|
|
* - `/:prefix/:plugin/:action/*` a plugin shortcut route.
|
2010-01-25 20:37:55 +00:00
|
|
|
* - `/:prefix/:plugin/:controller`
|
|
|
|
* - `/:prefix/:plugin/:controller/:action/*`
|
|
|
|
* - `/:prefix/:controller`
|
|
|
|
* - `/:prefix/:controller/:action/*`
|
2010-04-04 04:13:16 +00:00
|
|
|
*
|
|
|
|
* If plugins are found in your application the following routes are created:
|
|
|
|
*
|
|
|
|
* - `/:plugin` a plugin shortcut route.
|
|
|
|
* - `/:plugin/:action/*` a plugin shortcut route.
|
2010-01-25 20:37:55 +00:00
|
|
|
* - `/:plugin/:controller`
|
|
|
|
* - `/:plugin/:controller/:action/*`
|
2010-04-04 04:13:16 +00:00
|
|
|
*
|
|
|
|
* And lastly the following catch-all routes are connected.
|
|
|
|
*
|
2010-01-25 20:37:55 +00:00
|
|
|
* - `/:controller'
|
|
|
|
* - `/:controller/:action/*'
|
|
|
|
*
|
2010-04-04 04:13:16 +00:00
|
|
|
* 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
|
|
|
|
*/
|
2010-11-09 03:08:30 +00:00
|
|
|
private static function __connectDefaultRoutes() {
|
2011-05-09 04:31:00 +00:00
|
|
|
if ($plugins = CakePlugin::loaded()) {
|
2010-12-15 05:50:02 +00:00
|
|
|
App::uses('PluginShortRoute', 'Routing/Route');
|
2008-09-12 05:11:34 +00:00
|
|
|
foreach ($plugins as $key => $value) {
|
|
|
|
$plugins[$key] = Inflector::underscore($value);
|
|
|
|
}
|
2010-04-08 04:03:46 +00:00
|
|
|
$pluginPattern = implode('|', $plugins);
|
|
|
|
$match = array('plugin' => $pluginPattern);
|
2010-04-11 01:12:48 +00:00
|
|
|
$shortParams = array('routeClass' => 'PluginShortRoute', 'plugin' => $pluginPattern);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-04 04:26:02 +00:00
|
|
|
foreach (self::$_prefixes as $prefix) {
|
2009-09-27 04:12:03 +00:00
|
|
|
$params = array('prefix' => $prefix, $prefix => true);
|
2009-11-29 21:09:45 +00:00
|
|
|
$indexParams = $params + array('action' => 'index');
|
2010-05-04 04:26:02 +00:00
|
|
|
self::connect("/{$prefix}/:plugin", $indexParams, $shortParams);
|
|
|
|
self::connect("/{$prefix}/:plugin/:controller", $indexParams, $match);
|
|
|
|
self::connect("/{$prefix}/:plugin/:controller/:action/*", $params, $match);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-04 04:26:02 +00:00
|
|
|
self::connect('/:plugin', array('action' => 'index'), $shortParams);
|
|
|
|
self::connect('/:plugin/:controller', array('action' => 'index'), $match);
|
|
|
|
self::connect('/:plugin/:controller/:action/*', array(), $match);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-05-04 04:26:02 +00:00
|
|
|
foreach (self::$_prefixes as $prefix) {
|
2009-09-27 04:12:03 +00:00
|
|
|
$params = array('prefix' => $prefix, $prefix => true);
|
2009-11-29 21:09:45 +00:00
|
|
|
$indexParams = $params + array('action' => 'index');
|
2010-05-04 04:26:02 +00:00
|
|
|
self::connect("/{$prefix}/:controller", $indexParams);
|
|
|
|
self::connect("/{$prefix}/:controller/:action/*", $params);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-04 04:26:02 +00:00
|
|
|
self::connect('/:controller', array('action' => 'index'));
|
|
|
|
self::connect('/:controller/:action/*');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-03-02 11:20:45 +00:00
|
|
|
if (self::$_namedConfig['rules'] === false) {
|
2010-05-04 04:26:02 +00:00
|
|
|
self::connectNamed(true);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-04 04:26:02 +00:00
|
|
|
self::$_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
|
|
|
*
|
2010-05-02 06:16:16 +00:00
|
|
|
* Will accept either a CakeRequest object or an array of arrays. Support for
|
|
|
|
* accepting arrays may be removed in the future.
|
|
|
|
*
|
|
|
|
* @param mixed $params Parameters and path information or a CakeRequest object.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-02 05:59:56 +00:00
|
|
|
public static function setRequestInfo($request) {
|
|
|
|
if ($request instanceof CakeRequest) {
|
2010-05-04 04:26:02 +00:00
|
|
|
self::$_requests[] = $request;
|
2010-05-02 05:59:56 +00:00
|
|
|
} else {
|
|
|
|
$requestObj = new CakeRequest();
|
|
|
|
$request += array(array(), array());
|
|
|
|
$request[0] += array('controller' => false, 'action' => false, 'plugin' => null);
|
|
|
|
$requestObj->addParams($request[0])->addPaths($request[1]);
|
2010-05-04 04:26:02 +00:00
|
|
|
self::$_requests[] = $requestObj;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-05-02 05:43:37 +00:00
|
|
|
/**
|
|
|
|
* Get the either the current request object, or the first one.
|
|
|
|
*
|
|
|
|
* @param boolean $current Whether you want the request from the top of the stack or the first one.
|
|
|
|
* @return CakeRequest or null.
|
|
|
|
*/
|
|
|
|
public static function getRequest($current = false) {
|
|
|
|
if ($current) {
|
2010-05-04 04:26:02 +00:00
|
|
|
return self::$_requests[count(self::$_requests) - 1];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-04 04:26:02 +00:00
|
|
|
return isset(self::$_requests[0]) ? self::$_requests[0] : null;
|
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
|
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static function getParams($current = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($current) {
|
2010-05-04 04:26:02 +00:00
|
|
|
return self::$_requests[count(self::$_requests) - 1]->params;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-04 04:26:02 +00:00
|
|
|
if (isset(self::$_requests[0])) {
|
|
|
|
return self::$_requests[0]->params;
|
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
|
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static function getParam($name = 'controller', $current = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$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
|
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static function getPaths($current = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($current) {
|
2010-05-04 04:26:02 +00:00
|
|
|
return self::$_requests[count(self::$_requests) - 1];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-04 04:26:02 +00:00
|
|
|
if (!isset(self::$_requests[0])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return array('base' => null);
|
|
|
|
}
|
2010-05-04 04:26:02 +00:00
|
|
|
return array('base' => self::$_requests[0]->base);
|
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
|
|
|
*
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static function reload() {
|
2010-05-04 04:26:02 +00:00
|
|
|
if (empty(self::$_initialState)) {
|
|
|
|
self::$_initialState = get_class_vars('Router');
|
2010-12-22 04:14:17 +00:00
|
|
|
self::_setPrefixes();
|
2010-05-04 04:26:02 +00:00
|
|
|
return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-04 04:26:02 +00:00
|
|
|
foreach (self::$_initialState as $key => $val) {
|
|
|
|
if ($key != '_initialState') {
|
|
|
|
self::${$key} = $val;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-04 04:26:02 +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.
|
2011-01-18 22:23:12 +00:00
|
|
|
* @return boolean Returns false if no route exists at the position specified by $which.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static function promote($which = null) {
|
2008-09-14 17:17:49 +00:00
|
|
|
if ($which === null) {
|
2010-05-04 04:26:02 +00:00
|
|
|
$which = count(self::$routes) - 1;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-04 04:26:02 +00:00
|
|
|
if (!isset(self::$routes[$which])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-05-04 04:26:02 +00:00
|
|
|
$route =& self::$routes[$which];
|
|
|
|
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
|
|
|
*
|
2011-01-18 22:23:12 +00:00
|
|
|
* - Empty - the method will find address to actual 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
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static function url($url = null, $full = false) {
|
2011-04-22 19:52:42 +00:00
|
|
|
$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
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$path = array('base' => null);
|
2010-05-04 04:26:02 +00:00
|
|
|
if (!empty(self::$_requests)) {
|
2010-10-29 15:16:50 +00:00
|
|
|
$currentRequest = self::$_requests[count(self::$_requests) - 1];
|
|
|
|
if (!empty($currentRequest->params['requested'])) {
|
2010-05-04 04:26:02 +00:00
|
|
|
$request = self::$_requests[0];
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-10-29 15:16:50 +00:00
|
|
|
$request = $currentRequest;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-02 05:43:37 +00:00
|
|
|
$params = $request->params;
|
|
|
|
$path = array('base' => $request->base, 'here' => $request->here);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$base = $path['base'];
|
2011-04-22 19:52:42 +00:00
|
|
|
$extension = $output = $q = $frag = null;
|
2010-10-30 01:33:56 +00:00
|
|
|
|
|
|
|
if (empty($url)) {
|
2010-11-03 03:16:38 +00:00
|
|
|
$output = isset($path['here']) ? $path['here'] : '/';
|
|
|
|
if ($full && defined('FULL_BASE_URL')) {
|
|
|
|
$output = FULL_BASE_URL . $output;
|
|
|
|
}
|
|
|
|
return $output;
|
2010-10-30 01:33:56 +00:00
|
|
|
} elseif (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['#']);
|
|
|
|
}
|
2010-10-30 01:45:09 +00:00
|
|
|
if (isset($url['ext'])) {
|
|
|
|
$extension = '.' . $url['ext'];
|
|
|
|
unset($url['ext']);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
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
|
|
|
|
2010-05-04 04:26:02 +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
|
|
|
}
|
2011-02-24 21:43:13 +00:00
|
|
|
if (isset($url[$prefix]) && strpos($url['action'], $prefix . '_') === 0) {
|
2010-04-12 03:13:18 +00:00
|
|
|
$url['action'] = substr($url['action'], strlen($prefix) + 1);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
$match = false;
|
2009-10-08 04:35:06 +00:00
|
|
|
|
2010-05-04 04:26:02 +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
|
|
|
|
2010-05-04 04:26:02 +00:00
|
|
|
if (isset(self::$routes[$i]->options['persist'], $params)) {
|
|
|
|
$url = self::$routes[$i]->persistParams($url, $params);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-11-30 15:15:39 +00:00
|
|
|
|
2010-05-04 04:26:02 +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) {
|
2010-05-04 04:26:02 +00:00
|
|
|
$output = self::_handleNoRoute($url);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-10-30 01:45:09 +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;
|
|
|
|
}
|
2010-10-30 01:33:56 +00:00
|
|
|
if (substr($url, 0, 1) === '/') {
|
|
|
|
$output = substr($url, 1);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-05-04 04:26:02 +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;
|
|
|
|
}
|
|
|
|
}
|
2010-11-03 03:26:41 +00:00
|
|
|
$protocol = preg_match('#^[a-z][a-z0-9+-.]*\://#i', $output);
|
2010-10-30 02:10:24 +00:00
|
|
|
if ($protocol === 0) {
|
2010-10-30 01:37:03 +00:00
|
|
|
$output = str_replace('//', '/', $base . '/' . $output);
|
2010-10-30 01:33:56 +00:00
|
|
|
|
2010-10-30 01:37:03 +00:00
|
|
|
if ($full && defined('FULL_BASE_URL')) {
|
|
|
|
$output = FULL_BASE_URL . $output;
|
|
|
|
}
|
|
|
|
if (!empty($extension)) {
|
|
|
|
$output = rtrim($output, '/');
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-04 04:26:02 +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
|
|
|
|
* @see Router::url()
|
|
|
|
*/
|
2010-11-11 04:43:48 +00:00
|
|
|
protected static function _handleNoRoute($url) {
|
2010-12-19 22:09:34 +00:00
|
|
|
$named = $args = $query = array();
|
2009-12-02 02:56:03 +00:00
|
|
|
$skip = array_merge(
|
|
|
|
array('bare', 'action', 'controller', 'plugin', 'prefix'),
|
2010-05-04 04:26:02 +00:00
|
|
|
self::$_prefixes
|
2009-12-02 02:56:03 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$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++) {
|
2010-12-19 22:09:34 +00:00
|
|
|
$key = $keys[$i];
|
2009-12-02 02:56:03 +00:00
|
|
|
if (is_numeric($keys[$i])) {
|
2010-12-19 22:09:34 +00:00
|
|
|
$args[] = $url[$key];
|
2009-12-02 02:56:03 +00:00
|
|
|
} else {
|
2010-12-20 03:17:57 +00:00
|
|
|
$named[$key] = $url[$key];
|
2009-12-02 02:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list($args, $named) = array(Set::filter($args, true), Set::filter($named, true));
|
2010-05-04 04:26:02 +00:00
|
|
|
foreach (self::$_prefixes as $prefix) {
|
2009-12-02 02:56:03 +00:00
|
|
|
if (!empty($url[$prefix])) {
|
|
|
|
$url['action'] = str_replace($prefix . '_', '', $url['action']);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-19 22:09:34 +00:00
|
|
|
if (empty($named) && empty($args) && empty($query) && (!isset($url['action']) || $url['action'] === 'index')) {
|
2009-12-02 02:56:03 +00:00
|
|
|
$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
|
|
|
|
2010-04-04 20:34:07 +00:00
|
|
|
if (isset($url['plugin'])) {
|
2009-12-02 02:56:03 +00:00
|
|
|
array_unshift($urlOut, $url['plugin']);
|
|
|
|
}
|
|
|
|
|
2010-05-04 04:26:02 +00:00
|
|
|
foreach (self::$_prefixes as $prefix) {
|
2009-12-02 02:56:03 +00:00
|
|
|
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)) {
|
2010-12-18 19:16:00 +00:00
|
|
|
foreach ($named as $name => $value) {
|
2010-12-15 02:21:39 +00:00
|
|
|
if (is_array($value)) {
|
|
|
|
$flattend = Set::flatten($value, '][');
|
|
|
|
foreach ($flattend as $namedKey => $namedValue) {
|
2011-03-02 11:20:45 +00:00
|
|
|
$output .= '/' . $name . "[$namedKey]" . self::$_namedConfig['separator'] . $namedValue;
|
2010-12-15 02:21:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-03-02 11:20:45 +00:00
|
|
|
$output .= '/' . $name . self::$_namedConfig['separator'] . $value;
|
2010-12-15 02:21:39 +00:00
|
|
|
}
|
2009-12-02 02:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-19 22:09:34 +00:00
|
|
|
if (!empty($query)) {
|
|
|
|
$output .= Router::queryString($query);
|
|
|
|
}
|
2009-12-02 02:56:03 +00:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Generates a well-formed querystring from $q
|
|
|
|
*
|
2010-04-24 04:21:17 +00:00
|
|
|
* @param mixed $q Query string Either a string of already compiled query string arguments or
|
|
|
|
* an array of arguments to convert into a 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
|
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static 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
|
|
|
/**
|
2011-01-18 22:23:12 +00:00
|
|
|
* Reverses a parsed parameter array into a string. Works similarly to Router::url(), but
|
2009-12-28 04:09:26 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2010-08-05 03:35:49 +00:00
|
|
|
* This will strip out 'autoRender', 'bare', 'requested', and 'return' param names as those
|
|
|
|
* are used for CakePHP internals and should not normally be part of an output url.
|
|
|
|
*
|
2010-05-02 06:19:32 +00:00
|
|
|
* @param mixed $param The params array or CakeRequest object that needs to be reversed.
|
2011-04-04 20:11:47 +00:00
|
|
|
* @param boolean $full Set to true to include the full url including the protocol when reversing
|
|
|
|
* the url.
|
2009-12-28 04:09:26 +00:00
|
|
|
* @return string The string that is the reversed result of the array
|
|
|
|
*/
|
2011-04-04 20:11:47 +00:00
|
|
|
public static function reverse($params, $full = false) {
|
2010-05-02 06:19:32 +00:00
|
|
|
if ($params instanceof CakeRequest) {
|
2011-01-21 02:20:23 +00:00
|
|
|
$url = $params->query;
|
2010-05-02 06:19:32 +00:00
|
|
|
$params = $params->params;
|
2011-01-21 02:20:23 +00:00
|
|
|
} else {
|
|
|
|
$url = $params['url'];
|
2010-05-02 06:19:32 +00:00
|
|
|
}
|
2009-12-28 04:09:26 +00:00
|
|
|
$pass = $params['pass'];
|
|
|
|
$named = $params['named'];
|
2011-01-21 02:20:23 +00:00
|
|
|
|
2010-08-05 03:35:49 +00:00
|
|
|
unset(
|
|
|
|
$params['pass'], $params['named'], $params['paging'], $params['models'], $params['url'], $url['url'],
|
2011-05-17 00:35:27 +00:00
|
|
|
$params['autoRender'], $params['bare'], $params['requested'], $params['return'],
|
|
|
|
$params['_Token']
|
2010-08-05 03:35:49 +00:00
|
|
|
);
|
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
|
|
|
}
|
2011-04-04 20:11:47 +00:00
|
|
|
return Router::url($params, $full);
|
2009-12-28 04:09:26 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static function normalize($url = '/') {
|
2008-05-30 11:40:08 +00:00
|
|
|
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
|
|
|
}
|
2010-05-02 05:43:37 +00:00
|
|
|
$request = Router::getRequest();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-02 05:43:37 +00:00
|
|
|
if (!empty($request->base) && stristr($url, $request->base)) {
|
|
|
|
$url = preg_replace('/^' . preg_quote($request->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
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static function &requestRoute() {
|
2010-05-04 04:26:02 +00:00
|
|
|
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
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static function ¤tRoute() {
|
2010-05-04 04:26:02 +00:00
|
|
|
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
|
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static 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=...).
|
|
|
|
*
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-20 03:44:45 +00:00
|
|
|
public static function parseExtensions() {
|
2010-05-04 04:26:02 +00:00
|
|
|
self::$_parseExtensions = true;
|
2008-05-30 11:40:08 +00:00
|
|
|
if (func_num_args() > 0) {
|
2010-05-04 04:26:02 +00:00
|
|
|
self::$_validExtensions = func_get_args();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-04-20 03:44:45 +00:00
|
|
|
/**
|
|
|
|
* Get the list of extensions that can be parsed by Router. To add more
|
|
|
|
* extensions use Router::parseExtensions()
|
|
|
|
*
|
|
|
|
* @return array Array of extensions Router is configured to parse.
|
|
|
|
*/
|
|
|
|
public static function extensions() {
|
2010-05-04 04:26:02 +00:00
|
|
|
return self::$_validExtensions;
|
2010-04-20 03:44:45 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-09-30 13:49:39 +00:00
|
|
|
|
2010-05-04 04:35:22 +00:00
|
|
|
//Save the initial state
|
2010-11-01 01:56:59 +00:00
|
|
|
Router::reload();
|