Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 457 lines (425 sloc) 15.837 kB
e7f3c31 @gwoo going lithium
gwoo authored
1 <?php
2 /**
3 * Lithium: the most rad php framework
4 *
14de7bf @gwoo Happy 2012!
gwoo authored
5 * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
e7f3c31 @gwoo going lithium
gwoo authored
6 * @license http://opensource.org/licenses/bsd-license.php The BSD License
7 */
8
9e2105b @gwoo Moving \http to \net\http and \util\socket to \net\socket.
gwoo authored
9 namespace lithium\net\http;
e7f3c31 @gwoo going lithium
gwoo authored
10
dfe0c03 @nateabele Making dispatching infrastructure slightly less controller/action-cen…
nateabele authored
11 use lithium\util\Inflector;
5148f9f @nateabele WARNING: URLs which do not match routes now throw a `RoutingException`.
nateabele authored
12 use lithium\net\http\RoutingException;
e7f3c31 @gwoo going lithium
gwoo authored
13
9d1a9ad @nateabele Adding documentation to `\net\http\Router`, and replacing `Router::co…
nateabele authored
14 /**
15 * The two primary responsibilities of the `Router` class are to generate URLs from parameter lists,
16 * and to determine the correct set of dispatch parameters for incoming requests.
17 *
18 * Using `Route` objects, these two operations can be handled in a reciprocally consistent way.
19 * For example, if you wanted the `/login` URL to be routed to
2dddf3e @nateabele Removing completed `@todo`s, and reformatting paths to strip redundan…
nateabele authored
20 * `myapp\controllers\SessionsController::add()`, you could set up a route like the following in
21 * `config/routes.php`:
22 *
9d1a9ad @nateabele Adding documentation to `\net\http\Router`, and replacing `Router::co…
nateabele authored
23 * {{{
dfe0c03 @nateabele Making dispatching infrastructure slightly less controller/action-cen…
nateabele authored
24 * use lithium\net\http\Router;
9d1a9ad @nateabele Adding documentation to `\net\http\Router`, and replacing `Router::co…
nateabele authored
25 *
9658832 @nateabele Proof-of-concept to eliminate modification of arbitrary controller fo…
nateabele authored
26 * Router::connect('/login', array('controller' => 'Sessions', 'action' => 'add'));
2dddf3e @nateabele Removing completed `@todo`s, and reformatting paths to strip redundan…
nateabele authored
27 *
28 * // -- or --
29 *
30 * Router::connect('/login', 'Sessions::add');
31 * }}}
9d1a9ad @nateabele Adding documentation to `\net\http\Router`, and replacing `Router::co…
nateabele authored
32 *
2dddf3e @nateabele Removing completed `@todo`s, and reformatting paths to strip redundan…
nateabele authored
33 * Not only would that correctly route all requests for `/login` to `SessionsController::add()`, but
9d1a9ad @nateabele Adding documentation to `\net\http\Router`, and replacing `Router::co…
nateabele authored
34 * any time the framework generated a route with matching parameters, `Router` would return the
c8fb70b @nateabele Additional documentation for `\net\http\Router` and `Router::match()`.
nateabele authored
35 * correct short URL.
9d1a9ad @nateabele Adding documentation to `\net\http\Router`, and replacing `Router::co…
nateabele authored
36 *
c8fb70b @nateabele Additional documentation for `\net\http\Router` and `Router::match()`.
nateabele authored
37 * While most framework components that work with URLs (and utilize routing) handle calling the
38 * `Router` directly (i.e. controllers doing redirects, or helpers generating links), if you have a
39 * scenario where you need to call the `Router` directly, you can use the `match()` method.
40 *
41 * This allows you to keep your application's URL structure nicely decoupled from the underlying
42 * software design. For more information on parsing and generating URLs, see the `parse()` and
43 * `match()` methods.
9d1a9ad @nateabele Adding documentation to `\net\http\Router`, and replacing `Router::co…
nateabele authored
44 */
e7f3c31 @gwoo going lithium
gwoo authored
45 class Router extends \lithium\core\StaticObject {
46
313233b Docblocks for lithium\net.
psychic authored
47 /**
2dc40c9 @nateabele Implementing formatters for route parameters.
nateabele authored
48 * An array of loaded `Route` objects used to match Request objects against.
313233b Docblocks for lithium\net.
psychic authored
49 *
2dc40c9 @nateabele Implementing formatters for route parameters.
nateabele authored
50 * @see lithium\net\http\Route
313233b Docblocks for lithium\net.
psychic authored
51 * @var array
52 */
aaee21c @nateabele Small memory optimization to `\net\http\Router`.
nateabele authored
53 protected static $_configurations = array();
e7f3c31 @gwoo going lithium
gwoo authored
54
313233b Docblocks for lithium\net.
psychic authored
55 /**
2dc40c9 @nateabele Implementing formatters for route parameters.
nateabele authored
56 * An array of named closures matching up to corresponding route parameter values. Used to
57 * format those values.
58 *
59 * @see lithium\net\http\Router::formatters()
60 * @var array
61 */
62 protected static $_formatters = array();
63
64 /**
313233b Docblocks for lithium\net.
psychic authored
65 * Classes used by `Router`.
66 *
15bf6af @nateabele Removing `@author` and `@package` docblock tags. Tweaking XCache adap…
nateabele authored
67 * @var array
313233b Docblocks for lithium\net.
psychic authored
68 */
e7f3c31 @gwoo going lithium
gwoo authored
69 protected static $_classes = array(
15bf6af @nateabele Removing `@author` and `@package` docblock tags. Tweaking XCache adap…
nateabele authored
70 'route' => 'lithium\net\http\Route'
e7f3c31 @gwoo going lithium
gwoo authored
71 );
72
95e03fe @nateabele Refactoring route formatter handling, moving route Unicode test to co…
nateabele authored
73 /**
74 * Flag for generating Unicode-capable routes. Turn this off if you don't need it, or if you're
75 * using a broken OS distribution (i.e. CentOS).
76 */
77 protected static $_unicode = true;
78
79 /**
80 * Modify `Router` configuration settings and dependencies.
81 *
82 * @param array $config Optional array to override configuration. Acceptable keys are
83 * `'classes'` and `'unicode'`.
84 * @return array Returns the current configuration settings.
85 */
3d2bdce @nateabele Adding ability to configure class dependencies of `\net\http\Router`.…
nateabele authored
86 public static function config($config = array()) {
87 if (!$config) {
95e03fe @nateabele Refactoring route formatter handling, moving route Unicode test to co…
nateabele authored
88 return array('classes' => static::$_classes, 'unicode' => static::$_unicode);
3d2bdce @nateabele Adding ability to configure class dependencies of `\net\http\Router`.…
nateabele authored
89 }
90 if (isset($config['classes'])) {
91 static::$_classes = $config['classes'] + static::$_classes;
92 }
95e03fe @nateabele Refactoring route formatter handling, moving route Unicode test to co…
nateabele authored
93 if (isset($config['unicode'])) {
94 static::$_unicode = $config['unicode'];
95 }
3d2bdce @nateabele Adding ability to configure class dependencies of `\net\http\Router`.…
nateabele authored
96 }
97
9d1a9ad @nateabele Adding documentation to `\net\http\Router`, and replacing `Router::co…
nateabele authored
98 /**
15f7ee9 @nateabele Fixing class reference in `\action\Controller` docblock. Adding docs …
nateabele authored
99 * Connects a new route and returns the current routes array. This method creates a new
100 * `Route` object and registers it with the `Router`. The order in which routes are connected
101 * matters, since the order of precedence is taken into account in parsing and matching
102 * operations.
e7f3c31 @gwoo going lithium
gwoo authored
103 *
15f7ee9 @nateabele Fixing class reference in `\action\Controller` docblock. Adding docs …
nateabele authored
104 * @see lithium\net\http\Route
105 * @see lithium\net\http\Router::parse()
106 * @see lithium\net\http\Router::match()
3ebe6ba @alkemann hooks fixes for lithium/http
alkemann authored
107 * @param string $template An empty string, or a route string "/"
e7f3c31 @gwoo going lithium
gwoo authored
108 * @param array $params An array describing the default or required elements of the route
3ebe6ba @alkemann hooks fixes for lithium/http
alkemann authored
109 * @param array $options
e7f3c31 @gwoo going lithium
gwoo authored
110 * @return array Array of routes
111 */
4a3c3e4 @nateabele Implementing support for route handlers.
nateabele authored
112 public static function connect($template, $params = array(), $options = array()) {
2dc40c9 @nateabele Implementing formatters for route parameters.
nateabele authored
113 if (is_object($template)) {
114 return (static::$_configurations[] = $template);
115 }
116 if (is_string($params)) {
117 $params = static::_parseString($params, false);
118 }
119 if (isset($params[0]) && is_array($tmp = static::_parseString($params[0], false))) {
120 unset($params[0]);
121 $params = $tmp + $params;
e7f3c31 @gwoo going lithium
gwoo authored
122 }
9658832 @nateabele Proof-of-concept to eliminate modification of arbitrary controller fo…
nateabele authored
123 $params = static::_parseController($params);
124
2dc40c9 @nateabele Implementing formatters for route parameters.
nateabele authored
125 if (is_callable($options)) {
126 $options = array('handler' => $options);
e7f3c31 @gwoo going lithium
gwoo authored
127 }
2dc40c9 @nateabele Implementing formatters for route parameters.
nateabele authored
128 $config = compact('template', 'params') + $options + array(
95e03fe @nateabele Refactoring route formatter handling, moving route Unicode test to co…
nateabele authored
129 'formatters' => static::formatters(),
130 'unicode' => static::$_unicode
2dc40c9 @nateabele Implementing formatters for route parameters.
nateabele authored
131 );
132 return (static::$_configurations[] = static::_instance('route', $config));
e7f3c31 @gwoo going lithium
gwoo authored
133 }
134
135 /**
0c950e6 @nateabele Implementing persistent route parameters, refactored `\net\http\Route…
nateabele authored
136 * Wrapper method which takes a `Request` object, parses it through all attached `Route`
7efcd55 @mikegreiling Fixing a few typos and grammatical errors
mikegreiling authored
137 * objects, assigns the resulting parameters to the `Request` object, and returns it.
0c950e6 @nateabele Implementing persistent route parameters, refactored `\net\http\Route…
nateabele authored
138 *
139 * @param object $request A request object, usually an instance of `lithium\action\Request`.
140 * @return object Returns a copy of the `Request` object with parameters applied.
141 */
142 public static function process($request) {
4a3c3e4 @nateabele Implementing support for route handlers.
nateabele authored
143 if (!$result = static::parse($request)) {
0c950e6 @nateabele Implementing persistent route parameters, refactored `\net\http\Route…
nateabele authored
144 return $request;
145 }
807365a @nateabele Refactoring routing to give more control to individual `Route` classes.
nateabele authored
146 return $result;
0c950e6 @nateabele Implementing persistent route parameters, refactored `\net\http\Route…
nateabele authored
147 }
148
149 /**
2dc40c9 @nateabele Implementing formatters for route parameters.
nateabele authored
150 * Used to get or set an array of named formatter closures, which are used to format route
151 * parameters when generating URLs. For example, for controller/action parameters to be dashed
152 * instead of underscored or camelBacked, you could do the following:
153 *
154 * {{{
155 * use lithium\util\Inflector;
156 *
157 * Router::formatters(array(
158 * 'controller' => function($value) { return Inflector::slug($value); },
159 * 'action' => function($value) { return Inflector::slug($value); }
160 * ));
161 * }}}
162 *
163 * _Note_: Because formatters are copied to `Route` objects on an individual basis, make sure
164 * you append custom formatters _before_ connecting new routes.
165 *
166 * @param array $formatters An array of named formatter closures to append to (or overwrite) the
167 * existing list.
168 * @return array Returns the formatters array.
169 */
170 public static function formatters(array $formatters = array()) {
171 if (!static::$_formatters) {
172 static::$_formatters = array(
173 'args' => function($value) {
174 return is_array($value) ? join('/', $value) : $value;
175 },
176 'controller' => function($value) {
177 return Inflector::underscore($value);
178 }
179 );
180 }
181 if ($formatters) {
182 static::$_formatters = array_filter($formatters + static::$_formatters);
183 }
184 return static::$_formatters;
185 }
186
187 /**
9d1a9ad @nateabele Adding documentation to `\net\http\Router`, and replacing `Router::co…
nateabele authored
188 * Accepts an instance of `lithium\action\Request` (or a subclass) and matches it against each
e7f3c31 @gwoo going lithium
gwoo authored
189 * route, in the order that the routes are connected.
190 *
9d1a9ad @nateabele Adding documentation to `\net\http\Router`, and replacing `Router::co…
nateabele authored
191 * @see lithium\action\Request
9e2105b @gwoo Moving \http to \net\http and \util\socket to \net\socket.
gwoo authored
192 * @see lithium\net\http\Router::connect()
9d1a9ad @nateabele Adding documentation to `\net\http\Router`, and replacing `Router::co…
nateabele authored
193 * @param object $request A request object containing URL and environment data.
194 * @return array Returns an array of parameters specifying how the given request should be
195 * routed. The keys returned depend on the `Route` object that was matched, but
196 * typically include `'controller'` and `'action'` keys.
e7f3c31 @gwoo going lithium
gwoo authored
197 */
198 public static function parse($request) {
176fc65 @nateabele Implementing reverse continuation routing.
nateabele authored
199 $orig = $request->params;
200 $url = $request->url;
17524c1 @nateabele Implementing route continuations.
nateabele authored
201
aaee21c @nateabele Small memory optimization to `\net\http\Router`.
nateabele authored
202 foreach (static::$_configurations as $route) {
17524c1 @nateabele Implementing route continuations.
nateabele authored
203 if (!$match = $route->parse($request, compact('url'))) {
204 continue;
205 }
206 $request = $match;
207
208 if ($route->canContinue() && isset($request->params['args'])) {
209 $url = '/' . join('/', $request->params['args']);
210 unset($request->params['args']);
211 continue;
aaee21c @nateabele Small memory optimization to `\net\http\Router`.
nateabele authored
212 }
17524c1 @nateabele Implementing route continuations.
nateabele authored
213 return $request;
aaee21c @nateabele Small memory optimization to `\net\http\Router`.
nateabele authored
214 }
176fc65 @nateabele Implementing reverse continuation routing.
nateabele authored
215 $request->params = $orig;
e7f3c31 @gwoo going lithium
gwoo authored
216 }
217
7d72834 @nateabele Implementing parameter getter for `action\Request`, adding tests for …
nateabele authored
218 /**
219 * Attempts to match an array of route parameters (i.e. `'controller'`, `'action'`, etc.)
c8fb70b @nateabele Additional documentation for `\net\http\Router` and `Router::match()`.
nateabele authored
220 * against a connected `Route` object. For example, given the following route:
7d72834 @nateabele Implementing parameter getter for `action\Request`, adding tests for …
nateabele authored
221 *
c8fb70b @nateabele Additional documentation for `\net\http\Router` and `Router::match()`.
nateabele authored
222 * {{{
223 * Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
224 * }}}
225 *
226 * This will match:
227 * {{{
228 * $url = Router::match(array('controller' => 'users', 'action' => 'login'));
229 * // returns /login
230 * }}}
231 *
232 * For URLs templates with no insert parameters (i.e. elements like `{:id}` that are replaced
233 * with a value), all parameters must match exactly as they appear in the route parameters.
234 *
235 * Alternatively to using a full array, you can specify routes using a more compact syntax. The
236 * above example can be written as:
237 *
11e8b8b @nateabele Correcting typo in `Router` documentation.
nateabele authored
238 * {{{ $url = Router::match('Users::login'); // still returns /login }}}
c8fb70b @nateabele Additional documentation for `\net\http\Router` and `Router::match()`.
nateabele authored
239 *
240 * You can combine this with more complicated routes; for example:
241 * {{{
242 * Router::connect('/posts/{:id:\d+}', array('controller' => 'posts', 'action' => 'view'));
243 * }}}
244 *
245 * This will match:
246 * {{{
247 * $url = Router::match(array('controller' => 'posts', 'action' => 'view', 'id' => '1138'));
248 * // returns /posts/1138
249 * }}}
250 *
251 * Again, you can specify the same URL with a more compact syntax, as in the following:
252 * {{{
253 * $url = Router::match(array('Posts::view', 'id' => '1138'));
254 * // again, returns /posts/1138
255 * }}}
256 *
257 * You can use either syntax anywhere a URL is accepted, i.e.
258 * `lithium\action\Controller::redirect()`, or `lithium\template\helper\Html::link()`.
259 *
45bd6b2 @jperras Implementing Enhancement #59.
jperras authored
260 * @param string|array $url Options to match to a URL. Optionally, this can be a string
c8fb70b @nateabele Additional documentation for `\net\http\Router` and `Router::match()`.
nateabele authored
261 * containing a manually generated URL.
262 * @param object $context An instance of `lithium\action\Request`. This supplies the context for
263 * any persistent parameters, as well as the base URL for the application.
8de98e9 @nateabele Fixing coding standards violations. Closes ticket #143.
nateabele authored
264 * @param array $options Options for the generation of the matched URL. Currently accepted
265 * values are:
266 * - `'absolute'` _boolean_: Indicates whether or not the returned URL should be an
267 * absolute path (i.e. including scheme and host name).
268 * - `'host'` _string_: If `'absolute'` is `true`, sets the host name to be used,
269 * or overrides the one provided in `$context`.
270 * - `'scheme'` _string_: If `'absolute'` is `true`, sets the URL scheme to be
271 * used, or overrides the one provided in `$context`.
c8fb70b @nateabele Additional documentation for `\net\http\Router` and `Router::match()`.
nateabele authored
272 * @return string Returns a generated URL, based on the URL template of the matched route, and
273 * prefixed with the base URL of the application.
7d72834 @nateabele Implementing parameter getter for `action\Request`, adding tests for …
nateabele authored
274 */
45bd6b2 @jperras Implementing Enhancement #59.
jperras authored
275 public static function match($url = array(), $context = null, array $options = array()) {
176fc65 @nateabele Implementing reverse continuation routing.
nateabele authored
276 if (is_string($url = static::_prepareParams($url, $context, $options))) {
277 return $url;
2325e82 @nateabele Enabling use of parameters with routing shorthand syntax.
nateabele authored
278 }
65e9b52 @nateabele Allowing persistent route parameters to be overridden by setting to `…
nateabele authored
279 $defaults = array('action' => 'index');
45bd6b2 @jperras Implementing Enhancement #59.
jperras authored
280 $url += $defaults;
176fc65 @nateabele Implementing reverse continuation routing.
nateabele authored
281 $stack = array();
4a87b90 @jperras Updating `net\http\Router` to properly match routes with named anchors.
jperras authored
282
d256f91 @nateabele Fixing variable name changed in merge in `\net\http\Router::match()`.…
nateabele authored
283 $base = isset($context) ? $context->env('base') : '';
284 $suffix = isset($url['#']) ? "#{$url['#']}" : null;
285 unset($url['#']);
e7f3c31 @gwoo going lithium
gwoo authored
286
aaee21c @nateabele Small memory optimization to `\net\http\Router`.
nateabele authored
287 foreach (static::$_configurations as $route) {
d256f91 @nateabele Fixing variable name changed in merge in `\net\http\Router::match()`.…
nateabele authored
288 if (!$match = $route->match($url, $context)) {
289 continue;
aaee21c @nateabele Small memory optimization to `\net\http\Router`.
nateabele authored
290 }
176fc65 @nateabele Implementing reverse continuation routing.
nateabele authored
291 if ($route->canContinue()) {
292 $stack[] = $match;
293 $export = $route->export();
294 $keys = $export['match'] + $export['keys'] + $export['defaults'];
355b992 @nateabele Fixing conflicts between continuation routes and normal routes which …
nateabele authored
295 unset($keys['args']);
176fc65 @nateabele Implementing reverse continuation routing.
nateabele authored
296 $url = array_diff_key($url, $keys);
297 continue;
298 }
299 if ($stack) {
300 $stack[] = $match;
301 $match = static::_compileStack($stack);
302 }
d256f91 @nateabele Fixing variable name changed in merge in `\net\http\Router::match()`.…
nateabele authored
303 $path = rtrim("{$base}{$match}{$suffix}", '/') ?: '/';
304 $path = ($options) ? static::_prefix($path, $context, $options) : $path;
305 return $path ?: '/';
aaee21c @nateabele Small memory optimization to `\net\http\Router`.
nateabele authored
306 }
176fc65 @nateabele Implementing reverse continuation routing.
nateabele authored
307 $url = static::_formatError($url);
308 throw new RoutingException("No parameter match found for URL `{$url}`.");
309 }
310
311 protected static function _compileStack($stack) {
312 $result = null;
313
314 foreach (array_reverse($stack) as $fragment) {
315 if ($result) {
316 $result = str_replace('{:args}', ltrim($result, '/'), $fragment);
317 continue;
318 }
319 $result = $fragment;
320 }
321 return $result;
322 }
323
324 protected static function _formatError($url) {
cccd8b6 @nateabele Enhancing `RoutingException` messages to produce dumps of un-matched …
nateabele authored
325 $match = array("\n", 'array (', ',)', '=> NULL', '( \'', ', ');
326 $replace = array('', '(', ')', '=> null', '(\'', ', ');
176fc65 @nateabele Implementing reverse continuation routing.
nateabele authored
327 return str_replace($match, $replace, var_export($url, true));
328 }
329
9658832 @nateabele Proof-of-concept to eliminate modification of arbitrary controller fo…
nateabele authored
330 protected static function _parseController(array $params) {
331 if (!isset($params['controller'])) {
332 return $params;
333 }
334 if (strpos($params['controller'], '.')) {
335 $separated = explode('.', $params['controller'], 2);
336 list($params['library'], $params['controller']) = $separated;
337 }
338 if (strpos($params['controller'], '\\') === false) {
339 $params['controller'] = Inflector::camelize($params['controller']);
340 }
341 return $params;
342 }
343
176fc65 @nateabele Implementing reverse continuation routing.
nateabele authored
344 protected static function _prepareParams($url, $context, array $options) {
345 if (is_string($url)) {
346 if (strpos($url, '://')) {
347 return $url;
348 }
349 foreach (array('#', '//', 'mailto') as $prefix) {
350 if (strpos($url, $prefix) === 0) {
351 return $url;
352 }
353 }
354 if (is_string($url = static::_parseString($url, $context))) {
355 return static::_prefix($url, $context, $options);
356 }
357 }
358 if (isset($url[0]) && is_array($params = static::_parseString($url[0], $context))) {
359 unset($url[0]);
360 $url = $params + $url;
361 }
9658832 @nateabele Proof-of-concept to eliminate modification of arbitrary controller fo…
nateabele authored
362 return static::_persist(static::_parseController($url), $context);
e7f3c31 @gwoo going lithium
gwoo authored
363 }
364
313233b Docblocks for lithium\net.
psychic authored
365 /**
45bd6b2 @jperras Implementing Enhancement #59.
jperras authored
366 * Returns the prefix (scheme + hostname) for a URL based on the passed `$options` and the
367 * `$context`.
368 *
369 * @param string $path The URL to be prefixed.
370 * @param object $context The request context.
371 * @param array $options Options for generating the proper prefix. Currently accepted values
372 * are: `'absolute' => true|false`, `'host' => string` and `'scheme' => string`.
373 * @return string The prefixed URL, depending on the passed options.
374 */
375 protected static function _prefix($path, $context = null, array $options = array()) {
d256f91 @nateabele Fixing variable name changed in merge in `\net\http\Router::match()`.…
nateabele authored
376 $defaults = array('scheme' => null, 'host' => null, 'absolute' => false);
45bd6b2 @jperras Implementing Enhancement #59.
jperras authored
377
d256f91 @nateabele Fixing variable name changed in merge in `\net\http\Router::match()`.…
nateabele authored
378 if ($context) {
379 $defaults['host'] = $context->env('HTTP_HOST');
380 $defaults['scheme'] = $context->env('HTTPS') ? 'https://' : 'http://';
45bd6b2 @jperras Implementing Enhancement #59.
jperras authored
381 }
d256f91 @nateabele Fixing variable name changed in merge in `\net\http\Router::match()`.…
nateabele authored
382 $options += $defaults;
383
384 return ($options['absolute']) ? "{$options['scheme']}{$options['host']}{$path}" : $path;
385 }
45bd6b2 @jperras Implementing Enhancement #59.
jperras authored
386
d256f91 @nateabele Fixing variable name changed in merge in `\net\http\Router::match()`.…
nateabele authored
387 /**
388 * Copies persistent parameters (parameters in the request which have been designated to
389 * persist) to the current URL, unless the parameter has been explicitly disabled from
390 * persisting by setting the value in the URL to `null`, or by assigning some other value.
391 *
392 * For example:
393 *
394 * {{{ embed:lithium\tests\cases\net\http\RouterTest::testParameterPersistence(1-10) }}}
395 *
396 * @see lithium\action\Request::$persist
397 * @param array $url The parameters that define the URL to be matched.
398 * @param object $context Typically an instance of `lithium\action\Request`, which contains a
399 * `$persist` property, which is an array of keys to be persisted in URLs between
400 * requests.
401 * @return array Returns the modified URL array.
402 */
403 protected static function _persist($url, $context) {
404 if (!$context || !isset($context->persist)) {
405 return $url;
45bd6b2 @jperras Implementing Enhancement #59.
jperras authored
406 }
d256f91 @nateabele Fixing variable name changed in merge in `\net\http\Router::match()`.…
nateabele authored
407 foreach ($context->persist as $key) {
408 $url += array($key => $context->params[$key]);
45bd6b2 @jperras Implementing Enhancement #59.
jperras authored
409
d256f91 @nateabele Fixing variable name changed in merge in `\net\http\Router::match()`.…
nateabele authored
410 if ($url[$key] === null) {
411 unset($url[$key]);
412 }
413 }
414 return $url;
45bd6b2 @jperras Implementing Enhancement #59.
jperras authored
415 }
416
417 /**
313233b Docblocks for lithium\net.
psychic authored
418 * Returns a route from the loaded configurations, by name.
419 *
420 * @param string $route Name of the route to request.
c3f954f @jperras Removing bad EOL markers for \`net\http\{Route,Router}\`.
jperras authored
421 * @return lithium\net\http\Route
313233b Docblocks for lithium\net.
psychic authored
422 */
e7f3c31 @gwoo going lithium
gwoo authored
423 public static function get($route = null) {
0a87774 @nateabele Adding default caching configuration to bootstrap, refactoring cache …
nateabele authored
424 if ($route === null) {
aaee21c @nateabele Small memory optimization to `\net\http\Router`.
nateabele authored
425 return static::$_configurations;
e7f3c31 @gwoo going lithium
gwoo authored
426 }
aaee21c @nateabele Small memory optimization to `\net\http\Router`.
nateabele authored
427 return isset(static::$_configurations[$route]) ? static::$_configurations[$route] : null;
e7f3c31 @gwoo going lithium
gwoo authored
428 }
9d1a9ad @nateabele Adding documentation to `\net\http\Router`, and replacing `Router::co…
nateabele authored
429
430 /**
431 * Resets the `Router` to its default state, unloading all routes.
432 *
433 * @return void
434 */
435 public static function reset() {
aaee21c @nateabele Small memory optimization to `\net\http\Router`.
nateabele authored
436 static::$_configurations = array();
9d1a9ad @nateabele Adding documentation to `\net\http\Router`, and replacing `Router::co…
nateabele authored
437 }
0c950e6 @nateabele Implementing persistent route parameters, refactored `\net\http\Route…
nateabele authored
438
313233b Docblocks for lithium\net.
psychic authored
439 /**
440 * Helper function for taking a path string and parsing it into a controller and action array.
441 *
442 * @param string $path Path string to parse.
c3f954f @jperras Removing bad EOL markers for \`net\http\{Route,Router}\`.
jperras authored
443 * @param boolean $context
313233b Docblocks for lithium\net.
psychic authored
444 * @return array
445 */
2325e82 @nateabele Enabling use of parameters with routing shorthand syntax.
nateabele authored
446 protected static function _parseString($path, $context) {
0c950e6 @nateabele Implementing persistent route parameters, refactored `\net\http\Route…
nateabele authored
447 if (!preg_match('/^[A-Za-z0-9_]+::[A-Za-z0-9_]+$/', $path)) {
448 $base = $context ? $context->env('base') : '';
449 $path = trim($path, '/');
fd1d4b5 @nateabele Implementing shorthand syntax (+ shorthand with additional parameters…
nateabele authored
450 return $context !== false ? "{$base}/{$path}" : null;
0c950e6 @nateabele Implementing persistent route parameters, refactored `\net\http\Route…
nateabele authored
451 }
452 list($controller, $action) = explode('::', $path, 2);
453 return compact('controller', 'action');
454 }
e7f3c31 @gwoo going lithium
gwoo authored
455 }
456
457 ?>
Something went wrong with that request. Please try again.