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:

Show/Hide parent 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()

connect

top

Connects 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 three 'special' keys. pass, persist and routeClass 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 persistent parameters by redefining them in a url or remove them by setting the parameter to false. Ex. 'persist' => array('lang')

routeClass is used to extend and change how individual routes parse requests and handle reverse routing, via a custom routing class. Ex. 'routeClass' => 'SlugRoute'

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, supplying patterns for routing parameters and supplying the name of a custom routing class.

Method defined in:
cake/libs/router.php on line 263
See

routes

Return

array Array of routes

Access

public

Static

connectNamed

top

Specifies 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 339
Return

array

Access

public

Static

currentRoute

top

Returns the route matching the current request (useful for requestAction traces)

Method defined in:
cake/libs/router.php on line 1131
Return

CakeRoute Matching route object.

Access

public

Static

defaults

top

Tell 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 384
Return

void

Access

public

Static

getArgs

top

Takes a passed params and converts it to args

Parameters:
  • $args required

  • $options optional array ( )

Method defined in:
cake/libs/router.php on line 1191
Return

array Array containing passed and named parameters

Access

public

Static

getInstance

top

Gets 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

top

Takes 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 967
Return

array

Access

public

Static

getNamedExpressions

top

Gets 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

Router::$__named

Static

getParam

top

Gets 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 667
Return

string Parameter value

Access

public

Static

getParams

top

Gets parameter information

Parameters:
  • boolean $current optional false

    Get current request parameter, useful when using requestAction

Method defined in:
cake/libs/router.php on line 647
Return

array Parameter information

Access

public

Static

getPaths

top

Gets path information

Parameters:
  • boolean $current optional false

    Current parameter, useful when using requestAction

Method defined in:
cake/libs/router.php on line 683
Return

array

Access

public

Static

_handleNoRoute

top

A 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 900
Return

string A generated url for the array

Access

protected

See

Router::url()

mapResources

top

Creates 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 404
Return

void

Access

public

Static

matchNamed

top

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.

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 995
Return

boolean

Access

public

Static

normalize

top

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.

Parameters:
  • mixed $url optional '/'

    URL to normalize Either an array or a string url.

Method defined in:
cake/libs/router.php on line 1088
Return

string Normalized URL

Access

public

Static

parse

top

Parses 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 446
Return

array Parsed elements from URL

Access

public

Static

parseExtensions

top

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=...).

Method defined in:
cake/libs/router.php on line 1175
Access

public

Return

void

Static

prefixes

top

Returns the list of prefixes used in connected routes

Method defined in:
cake/libs/router.php on line 432
Return

array A list of prefixes used in connected routes

Access

public

Static

promote

top

Promote 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 719
Return

boolean Returns false if no route exists at the position specified by $which.

Access

public

Static

queryString

top

Generates 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 1027
Return

array

Access

public

Static

reload

top

Reloads default Router settings. Resets all class variables and removes all connected routes.

Method defined in:
cake/libs/router.php on line 702
Access

public

Return

void

Static

requestRoute

top

Returns the route matching the current request URL.

Method defined in:
cake/libs/router.php on line 1119
Return

CakeRoute Matching route object.

Access

public

Static

reverse

top

Reverses a parsed parameter array into a string. Works similarly 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.

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.

Parameters:
  • $params required

Method defined in:
cake/libs/router.php on line 1063
Return

string The string that is the reversed result of the array

Access

public

Static

Router

top

Constructor for Router. Builds __prefixes

Method defined in:
cake/libs/router.php on line 159
Return

void

setRequestInfo

top

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.

Parameters:
  • array $params required

    Parameters and path information

Method defined in:
cake/libs/router.php on line 623
Return

void

Access

public

Static

stripPlugin

top

Removes 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 1145
Return

base url with plugin name removed if present

Access

public

Static

url

top

Finds 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 actual 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 the FULL_BASE_URL constant 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 764
Return

string Full translated URL with base path.

Access

public

Static