/** * 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 four 'special' keys. `pass`, `named`, `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'` * * `named` is used to configure named parameters at the route level. This key uses the same options * as Router::connectNamed() * * @param string $route A string describing the template of the route * @param array $defaults 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. * @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, supplying patterns for routing parameters and supplying the name of a * custom routing class. * @see routes * @return array Array of routes * @throws RouterException */ public static function connect($route, $defaults = array(), $options = array()) { foreach (self::$_prefixes as $prefix) { if (isset($defaults[$prefix])) { $defaults['prefix'] = $prefix; break; } } if (isset($defaults['prefix'])) { self::$_prefixes[] = $defaults['prefix']; self::$_prefixes = array_keys(array_flip(self::$_prefixes)); } $defaults += array('plugin' => null); if (empty($options['action'])) { $defaults += array('action' => 'index'); } $routeClass = 'CakeRoute'; if (isset($options['routeClass'])) { $routeClass = $options['routeClass']; if (!is_subclass_of($routeClass, 'CakeRoute')) { throw new RouterException(__('Route classes must extend CakeRoute')); } unset($options['routeClass']); if ($routeClass == 'RedirectRoute' && isset($defaults['redirect'])) { $defaults = $defaults['redirect']; } } self::$routes[] = new $routeClass($route, $defaults, $options); return self::$routes; }
/** * 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 of regular expressions that are used to match requests to destinations. * * Examples: * * `Router::connect('/:controller/:action/*');` * * The first token ':controller' 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. * * $defaults is merged with the results of parsing the request URL to form the final routing destination and its * parameters. This destination is expressed as an associative array by Router. See the output of {@link parse()}. * * $options offers four 'special' keys. `pass`, `named`, `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'` * - `named` is used to configure named parameters at the route level. This key uses the same options * as Router::connectNamed() * * You can also add additional conditions for matching routes to the $defaults array. * The following conditions can be used: * * - `[type]` Only match requests for specific content types. * - `[method]` Only match requests with specific HTTP verbs. * - `[server]` Only match when $_SERVER['SERVER_NAME'] matches the given value. * * Example of using the `[method]` condition: * * `Router::connect('/tasks', array('controller' => 'tasks', 'action' => 'index', '[method]' => 'GET'));` * * The above route will only be matched for GET requests. POST requests will fail to match this route. * * @param string $route A string describing the template of the route * @param array $defaults 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. * @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, supplying patterns for routing parameters and supplying the name of a * custom routing class. * @see routes * @see parse(). * @return array Array of routes * @throws RouterException */ public static function connect($route, $defaults = array(), $options = array()) { self::$initialized = true; foreach (self::$_prefixes as $prefix) { if (isset($defaults[$prefix])) { if ($defaults[$prefix]) { $defaults['prefix'] = $prefix; } else { unset($defaults[$prefix]); } break; } } if (isset($defaults['prefix'])) { self::$_prefixes[] = $defaults['prefix']; self::$_prefixes = array_keys(array_flip(self::$_prefixes)); } $defaults += array('plugin' => null); if (empty($options['action'])) { $defaults += array('action' => 'index'); } $routeClass = self::$_routeClass; if (isset($options['routeClass'])) { if (strpos($options['routeClass'], '.') === false) { $routeClass = $options['routeClass']; } else { list(, $routeClass) = pluginSplit($options['routeClass'], true); } $routeClass = self::_validateRouteClass($routeClass); unset($options['routeClass']); } if ($routeClass === 'RedirectRoute' && isset($defaults['redirect'])) { $defaults = $defaults['redirect']; } self::$routes[] = new $routeClass($route, $defaults, $options); return self::$routes; }
/** * Sets the Routing prefixes. * * @return void */ protected static function _setPrefixes() { $routing = Configure::read('Routing'); if (!empty($routing['prefixes'])) { self::$_prefixes = array_merge(self::$_prefixes, (array) $routing['prefixes']); } }