Router Class Info:
- Class Declaration:
class Router
- File name:
- cake/libs/router.php
- Description:
Parses the request URL into controller, action, and parameters.
- Package
- cake
- Subpackage
- cake.cake.libs
Properties:
-
named string
Stores all information necessary to decide what named arguments are parsed under what conditions.
-
routes array
Array of routes connected with Router::connect()
Method Summary:
- connect( $route, $defaults = array ( ), $options = array ( ) )
- connectNamed( $named, $options = array ( ) )
- currentRoute( )
- defaults( $connect = true )
- getArgs( $args, $options = array ( ) )
- getInstance( )
- getNamedElements( $params, $controller = NULL, $action = NULL )
- getNamedExpressions( )
- getParam( $name = 'controller', $current = false )
- getParams( $current = false )
- getPaths( $current = false )
- _handleNoRoute( $url )
- mapResources( $controller, $options = array ( ) )
- matchNamed( $param, $val, $rule, $context = array ( ) )
- normalize( $url = '/' )
- parse( $url )
- parseExtensions( )
- prefixes( )
- promote( $which = NULL )
- queryString( $q, $extra = array ( ), $escape = false )
- reload( )
- requestRoute( )
- reverse( $params )
- Router( )
- setRequestInfo( $params )
- stripPlugin( $base, $plugin = NULL )
- url( $url = NULL, $full = false )
connect
topConnects a new Route in the router.
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/*');
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
like /posts/edit/1/foo/bar.
Router::connect('/home-page', array('controller' => 'pages', 'action' => 'display', 'home'));
The above shows the use of route parameter defaults. And providing routing parameters for a static route.
Router::connect(
'/:lang/:controller/:action/:id',
array(),
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.
$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')
- Parameters:
-
-
string $route required
A string describing the template of the route
-
array $defaults optional array ( )
An array describing the default route parameters. These parameters will be used by default and can supply routing parameters that are not dynamic. See above.
-
array $options optional array ( )
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.
-
- Method defined in:
- cake/libs/router.php on line 259
- See
routes
- Return
array Array of routes
- Access
public
- Static
connectNamed
topSpecifies what named parameters CakePHP should be parsing. The most common setups are:
Do not parse any named parameters:
Router::connectNamed(false);
Parse only default parameters used for CakePHP's pagination:
Router::connectNamed(false, array('default' => true));
Parse only the page parameter if its value is a number:
Router::connectNamed(array('page' => '[\d]+'), array('default' => false, 'greedy' => false));
Parse only the page parameter no mater what.
Router::connectNamed(array('page'), array('default' => false, 'greedy' => false));
Parse only the page parameter if the current action is 'index'.
Router::connectNamed(
array('page' => array('action' => 'index')),
array('default' => false, 'greedy' => false)
);
Parse only the page parameter if the current action is 'index' and the controller is 'pages'.
Router::connectNamed(
array('page' => array('action' => 'index', 'controller' => 'pages')),
array('default' => false, 'greedy' => false)
);
- Parameters:
-
-
array $named required
A list of named parameters. Key value pairs are accepted where values are either regex strings to match, or arrays as seen above.
-
array $options optional array ( )
Allows to control all settings: separator, greedy, reset, default
-
- Method defined in:
- cake/libs/router.php on line 332
- Return
array
- Access
public
- Static
currentRoute
topReturns the route matching the current request (useful for requestAction traces)
- Method defined in:
- cake/libs/router.php on line 1118
- Return
CakeRoute Matching route object.
- Access
public
- Static
defaults
topTell router to connect or not connect the default routes.
If default routes are disabled all automatic route generation will be disabled and you will need to manually configure all the routes you want.
- Parameters:
-
-
boolean $connect optional true
Set to true or false depending on whether you want or don't want default routes.
-
- Method defined in:
- cake/libs/router.php on line 377
- Return
void
- Access
public
- Static
getArgs
topTakes an passed params and converts it to args
- Parameters:
-
-
$args required
-
$options optional array ( )
-
- Method defined in:
- cake/libs/router.php on line 1178
- Return
array Array containing passed and named parameters
- Access
public
- Static
getInstance
topGets a reference to the Router object instance
- Method defined in:
- cake/libs/router.php on line 188
- Return
Router Instance of the Router.
- Access
public
- Static
getNamedElements
topTakes an array of URL parameters and separates the ones that can be used as named arguments
- Parameters:
-
-
array $params required
Associative array of URL parameters.
-
string $controller optional NULL
Name of controller being routed. Used in scoping.
-
string $action optional NULL
Name of action being routed. Used in scoping.
-
- Method defined in:
- cake/libs/router.php on line 960
- Return
array
- Access
public
- Static
getNamedExpressions
topGets the named route elements for use in app/config/routes.php
- Method defined in:
- cake/libs/router.php on line 205
- Return
array Named route elements
- Access
public
- See
- Static
getParam
topGets URL parameter by name
- Parameters:
-
-
string $name optional 'controller'
Parameter name
-
boolean $current optional false
Current parameter, useful when using requestAction
-
- Method defined in:
- cake/libs/router.php on line 660
- Return
string Parameter value
- Access
public
- Static
getParams
topGets parameter information
- Parameters:
-
-
boolean $current optional false
Get current request parameter, useful when using requestAction
-
- Method defined in:
- cake/libs/router.php on line 640
- Return
array Parameter information
- Access
public
- Static
getPaths
topGets path information
- Parameters:
-
-
boolean $current optional false
Current parameter, useful when using requestAction
-
- Method defined in:
- cake/libs/router.php on line 676
- Return
array
- Access
public
- Static
_handleNoRoute
topA special fallback method that handles url arrays that cannot match any defined routes.
- Parameters:
-
-
array $url required
A url that didn't match any routes
-
- Method defined in:
- cake/libs/router.php on line 893
- Return
string A generated url for the array
- Access
protected
- See
mapResources
topCreates REST resource routes for the given controller(s)
Options:
- '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 '/'.
- Parameters:
-
-
mixed $controller required
A controller name or array of controller names (i.e. "Posts" or "ListItems")
-
array $options optional array ( )
Options to use when generating REST routes
-
- Method defined in:
- cake/libs/router.php on line 397
- Return
void
- Access
public
- Static
matchNamed
topReturn 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.
- Parameters:
-
-
string $param required
The name of the named parameter
-
string $val required
The value of the named parameter
-
array $rule required
The rule(s) to apply, can also be a match string
-
string $context optional array ( )
An array with additional context information (controller / action)
-
- Method defined in:
- cake/libs/router.php on line 988
- Return
boolean
- Access
public
- Static
normalize
topNormalizes 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.
- Parameters:
-
-
mixed $url optional '/'
URL to normalize Either an array or a string url.
-
- Method defined in:
- cake/libs/router.php on line 1075
- Return
string Normalized URL
- Access
public
- Static
parse
topParses given URL and returns an array of controller, action and parameters taken from that URL.
- Parameters:
-
-
string $url required
URL to be parsed
-
- Method defined in:
- cake/libs/router.php on line 439
- Return
array Parsed elements from URL
- Access
public
- Static
parseExtensions
topInstructs 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=...).
- Method defined in:
- cake/libs/router.php on line 1162
- Access
public
- Return
void
- Static
prefixes
topReturns the list of prefixes used in connected routes
- Method defined in:
- cake/libs/router.php on line 425
- Return
array A list of prefixes used in connected routes
- Access
public
- Static
promote
topPromote a route (by default, the last one added) to the beginning of the list
- Parameters:
-
-
$which optional NULL
-
- Method defined in:
- cake/libs/router.php on line 712
- Return
boolean Retuns false if no route exists at the position specified by $which.
- Access
public
- Static
queryString
topGenerates a well-formed querystring from $q
- Parameters:
-
-
mixed $q required
Query string
-
array $extra optional array ( )
Extra querystring parameters.
-
bool $escape optional false
Whether or not to use escaped &
-
- Method defined in:
- cake/libs/router.php on line 1020
- Return
array
- Access
public
- Static
reload
topReloads default Router settings. Resets all class variables and removes all connected routes.
- Method defined in:
- cake/libs/router.php on line 695
- Access
public
- Return
void
- Static
requestRoute
topReturns the route matching the current request URL.
- Method defined in:
- cake/libs/router.php on line 1106
- Return
CakeRoute Matching route object.
- Access
public
- Static
reverse
topReverses 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.
- Parameters:
-
-
$params required
-
- Method defined in:
- cake/libs/router.php on line 1053
- Return
string The string that is the reversed result of the array
- Access
public
- Static
Router
topConstructor for Router. Builds __prefixes
- Method defined in:
- cake/libs/router.php on line 159
- Return
void
setRequestInfo
topTakes 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.
- Parameters:
-
-
array $params required
Parameters and path information
-
- Method defined in:
- cake/libs/router.php on line 616
- Return
void
- Access
public
- Static
stripPlugin
topRemoves the plugin name from the base URL.
- Parameters:
-
-
string $base required
Base URL
-
string $plugin optional NULL
Plugin name
-
- Method defined in:
- cake/libs/router.php on line 1132
- Return
base url with plugin name removed if present
- Access
public
- Static
url
topFinds URL for specified action.
Returns an URL pointing to a combination of controller and action. Param $url can be:
- Empty - the method will find address to actuall controller/action.
- '/' - the method will find base URL of application.
- A combination of controller/action - the method will find url for it.
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 theFULL_BASE_URLconstant will be prepended to generated urls.
- Parameters:
-
-
mixed $url optional NULL
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)
-
mixed $full optional false
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.
-
- Method defined in:
- cake/libs/router.php on line 757
- Return
string Full translated URL with base path.
- Access
public
- Static