| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
4 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
5 |
*
|
|
|
6 |
* Licensed under The MIT License
|
|
|
7 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
8 |
* Redistributions of files must retain the above copyright notice.
|
|
|
9 |
*
|
|
|
10 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
11 |
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
12 |
* @since CakePHP(tm) v 1.3
|
|
|
13 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
App::uses('Hash', 'Utility');
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* A single Route used by the Router to connect requests to
|
|
|
20 |
* parameter maps.
|
|
|
21 |
*
|
|
|
22 |
* Not normally created as a standalone. Use Router::connect() to create
|
|
|
23 |
* Routes for your application.
|
|
|
24 |
*
|
|
|
25 |
* @package Cake.Routing.Route
|
|
|
26 |
*/
|
|
|
27 |
class CakeRoute {
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* An array of named segments in a Route.
|
|
|
31 |
* `/:controller/:action/:id` has 3 key elements
|
|
|
32 |
*
|
|
|
33 |
* @var array
|
|
|
34 |
*/
|
|
|
35 |
public $keys = array();
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* An array of additional parameters for the Route.
|
|
|
39 |
*
|
|
|
40 |
* @var array
|
|
|
41 |
*/
|
|
|
42 |
public $options = array();
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Default parameters for a Route
|
|
|
46 |
*
|
|
|
47 |
* @var array
|
|
|
48 |
*/
|
|
|
49 |
public $defaults = array();
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* The routes template string.
|
|
|
53 |
*
|
|
|
54 |
* @var string
|
|
|
55 |
*/
|
|
|
56 |
public $template = null;
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Is this route a greedy route? Greedy routes have a `/*` in their
|
|
|
60 |
* template
|
|
|
61 |
*
|
|
|
62 |
* @var string
|
|
|
63 |
*/
|
|
|
64 |
protected $_greedy = false;
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* The compiled route regular expression
|
|
|
68 |
*
|
|
|
69 |
* @var string
|
|
|
70 |
*/
|
|
|
71 |
protected $_compiledRoute = null;
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* HTTP header shortcut map. Used for evaluating header-based route expressions.
|
|
|
75 |
*
|
|
|
76 |
* @var array
|
|
|
77 |
*/
|
|
|
78 |
protected $_headerMap = array(
|
|
|
79 |
'type' => 'content_type',
|
|
|
80 |
'method' => 'request_method',
|
|
|
81 |
'server' => 'server_name'
|
|
|
82 |
);
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Constructor for a Route
|
|
|
86 |
*
|
|
|
87 |
* @param string $template Template string with parameter placeholders
|
|
|
88 |
* @param array $defaults Array of defaults for the route.
|
|
|
89 |
* @param array $options Array of additional options for the Route
|
|
|
90 |
*/
|
|
|
91 |
public function __construct($template, $defaults = array(), $options = array()) {
|
|
|
92 |
$this->template = $template;
|
|
|
93 |
$this->defaults = (array)$defaults;
|
|
|
94 |
$this->options = (array)$options;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Check if a Route has been compiled into a regular expression.
|
|
|
99 |
*
|
|
|
100 |
* @return boolean
|
|
|
101 |
*/
|
|
|
102 |
public function compiled() {
|
|
|
103 |
return !empty($this->_compiledRoute);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Compiles the route's regular expression.
|
|
|
108 |
*
|
|
|
109 |
* Modifies defaults property so all necessary keys are set
|
|
|
110 |
* and populates $this->names with the named routing elements.
|
|
|
111 |
*
|
|
|
112 |
* @return array Returns a string regular expression of the compiled route.
|
|
|
113 |
*/
|
|
|
114 |
public function compile() {
|
|
|
115 |
if ($this->compiled()) {
|
|
|
116 |
return $this->_compiledRoute;
|
|
|
117 |
}
|
|
|
118 |
$this->_writeRoute();
|
|
|
119 |
return $this->_compiledRoute;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Builds a route regular expression.
|
|
|
124 |
*
|
|
|
125 |
* Uses the template, defaults and options properties to compile a
|
|
|
126 |
* regular expression that can be used to parse request strings.
|
|
|
127 |
*
|
|
|
128 |
* @return void
|
|
|
129 |
*/
|
|
|
130 |
protected function _writeRoute() {
|
|
|
131 |
if (empty($this->template) || ($this->template === '/')) {
|
|
|
132 |
$this->_compiledRoute = '#^/*$#';
|
|
|
133 |
$this->keys = array();
|
|
|
134 |
return;
|
|
|
135 |
}
|
|
|
136 |
$route = $this->template;
|
|
|
137 |
$names = $routeParams = array();
|
|
|
138 |
$parsed = preg_quote($this->template, '#');
|
|
|
139 |
|
|
|
140 |
preg_match_all('#:([A-Za-z0-9_-]+[A-Z0-9a-z])#', $route, $namedElements);
|
|
|
141 |
foreach ($namedElements[1] as $i => $name) {
|
|
|
142 |
$search = '\\' . $namedElements[0][$i];
|
|
|
143 |
if (isset($this->options[$name])) {
|
|
|
144 |
$option = null;
|
|
|
145 |
if ($name !== 'plugin' && array_key_exists($name, $this->defaults)) {
|
|
|
146 |
$option = '?';
|
|
|
147 |
}
|
|
|
148 |
$slashParam = '/\\' . $namedElements[0][$i];
|
|
|
149 |
if (strpos($parsed, $slashParam) !== false) {
|
|
|
150 |
$routeParams[$slashParam] = '(?:/(?P<' . $name . '>' . $this->options[$name] . ')' . $option . ')' . $option;
|
|
|
151 |
} else {
|
|
|
152 |
$routeParams[$search] = '(?:(?P<' . $name . '>' . $this->options[$name] . ')' . $option . ')' . $option;
|
|
|
153 |
}
|
|
|
154 |
} else {
|
|
|
155 |
$routeParams[$search] = '(?:(?P<' . $name . '>[^/]+))';
|
|
|
156 |
}
|
|
|
157 |
$names[] = $name;
|
|
|
158 |
}
|
|
|
159 |
if (preg_match('#\/\*\*$#', $route)) {
|
|
|
160 |
$parsed = preg_replace('#/\\\\\*\\\\\*$#', '(?:/(?P<_trailing_>.*))?', $parsed);
|
|
|
161 |
$this->_greedy = true;
|
|
|
162 |
}
|
|
|
163 |
if (preg_match('#\/\*$#', $route)) {
|
|
|
164 |
$parsed = preg_replace('#/\\\\\*$#', '(?:/(?P<_args_>.*))?', $parsed);
|
|
|
165 |
$this->_greedy = true;
|
|
|
166 |
}
|
|
|
167 |
krsort($routeParams);
|
|
|
168 |
$parsed = str_replace(array_keys($routeParams), array_values($routeParams), $parsed);
|
|
|
169 |
$this->_compiledRoute = '#^' . $parsed . '[/]*$#';
|
|
|
170 |
$this->keys = $names;
|
|
|
171 |
|
|
|
172 |
// Remove defaults that are also keys. They can cause match failures
|
|
|
173 |
foreach ($this->keys as $key) {
|
|
|
174 |
unset($this->defaults[$key]);
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
/**
|
|
|
179 |
* Checks to see if the given URL can be parsed by this route.
|
|
|
180 |
*
|
|
|
181 |
* If the route can be parsed an array of parameters will be returned; if not
|
|
|
182 |
* false will be returned. String URLs are parsed if they match a routes regular expression.
|
|
|
183 |
*
|
|
|
184 |
* @param string $url The URL to attempt to parse.
|
|
|
185 |
* @return mixed Boolean false on failure, otherwise an array or parameters
|
|
|
186 |
*/
|
|
|
187 |
public function parse($url) {
|
|
|
188 |
if (!$this->compiled()) {
|
|
|
189 |
$this->compile();
|
|
|
190 |
}
|
|
|
191 |
if (!preg_match($this->_compiledRoute, urldecode($url), $route)) {
|
|
|
192 |
return false;
|
|
|
193 |
}
|
|
|
194 |
foreach ($this->defaults as $key => $val) {
|
|
|
195 |
$key = (string)$key;
|
|
|
196 |
if ($key[0] === '[' && preg_match('/^\[(\w+)\]$/', $key, $header)) {
|
|
|
197 |
if (isset($this->_headerMap[$header[1]])) {
|
|
|
198 |
$header = $this->_headerMap[$header[1]];
|
|
|
199 |
} else {
|
|
|
200 |
$header = 'http_' . $header[1];
|
|
|
201 |
}
|
|
|
202 |
$header = strtoupper($header);
|
|
|
203 |
|
|
|
204 |
$val = (array)$val;
|
|
|
205 |
$h = false;
|
|
|
206 |
|
|
|
207 |
foreach ($val as $v) {
|
|
|
208 |
if (env($header) === $v) {
|
|
|
209 |
$h = true;
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
if (!$h) {
|
|
|
213 |
return false;
|
|
|
214 |
}
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
array_shift($route);
|
|
|
218 |
$count = count($this->keys);
|
|
|
219 |
for ($i = 0; $i <= $count; $i++) {
|
|
|
220 |
unset($route[$i]);
|
|
|
221 |
}
|
|
|
222 |
$route['pass'] = $route['named'] = array();
|
|
|
223 |
|
|
|
224 |
// Assign defaults, set passed args to pass
|
|
|
225 |
foreach ($this->defaults as $key => $value) {
|
|
|
226 |
if (isset($route[$key])) {
|
|
|
227 |
continue;
|
|
|
228 |
}
|
|
|
229 |
if (is_int($key)) {
|
|
|
230 |
$route['pass'][] = $value;
|
|
|
231 |
continue;
|
|
|
232 |
}
|
|
|
233 |
$route[$key] = $value;
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
foreach ($this->keys as $key) {
|
|
|
237 |
if (isset($route[$key])) {
|
|
|
238 |
$route[$key] = rawurldecode($route[$key]);
|
|
|
239 |
}
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
if (isset($route['_args_'])) {
|
|
|
243 |
list($pass, $named) = $this->_parseArgs($route['_args_'], $route);
|
|
|
244 |
$route['pass'] = array_merge($route['pass'], $pass);
|
|
|
245 |
$route['named'] = $named;
|
|
|
246 |
unset($route['_args_']);
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
if (isset($route['_trailing_'])) {
|
|
|
250 |
$route['pass'][] = rawurldecode($route['_trailing_']);
|
|
|
251 |
unset($route['_trailing_']);
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
// restructure 'pass' key route params
|
|
|
255 |
if (isset($this->options['pass'])) {
|
|
|
256 |
$j = count($this->options['pass']);
|
|
|
257 |
while ($j--) {
|
|
|
258 |
if (isset($route[$this->options['pass'][$j]])) {
|
|
|
259 |
array_unshift($route['pass'], $route[$this->options['pass'][$j]]);
|
|
|
260 |
}
|
|
|
261 |
}
|
|
|
262 |
}
|
|
|
263 |
return $route;
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
/**
|
|
|
267 |
* Parse passed and Named parameters into a list of passed args, and a hash of named parameters.
|
|
|
268 |
* The local and global configuration for named parameters will be used.
|
|
|
269 |
*
|
|
|
270 |
* @param string $args A string with the passed & named params. eg. /1/page:2
|
|
|
271 |
* @param string $context The current route context, which should contain controller/action keys.
|
|
|
272 |
* @return array Array of ($pass, $named)
|
|
|
273 |
*/
|
|
|
274 |
protected function _parseArgs($args, $context) {
|
|
|
275 |
$pass = $named = array();
|
|
|
276 |
$args = explode('/', $args);
|
|
|
277 |
|
|
|
278 |
$namedConfig = Router::namedConfig();
|
|
|
279 |
$greedy = $namedConfig['greedyNamed'];
|
|
|
280 |
$rules = $namedConfig['rules'];
|
|
|
281 |
if (!empty($this->options['named'])) {
|
|
|
282 |
$greedy = isset($this->options['greedyNamed']) && $this->options['greedyNamed'] === true;
|
|
|
283 |
foreach ((array)$this->options['named'] as $key => $val) {
|
|
|
284 |
if (is_numeric($key)) {
|
|
|
285 |
$rules[$val] = true;
|
|
|
286 |
continue;
|
|
|
287 |
}
|
|
|
288 |
$rules[$key] = $val;
|
|
|
289 |
}
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
foreach ($args as $param) {
|
|
|
293 |
if (empty($param) && $param !== '0' && $param !== 0) {
|
|
|
294 |
continue;
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
$separatorIsPresent = strpos($param, $namedConfig['separator']) !== false;
|
|
|
298 |
if ((!isset($this->options['named']) || !empty($this->options['named'])) && $separatorIsPresent) {
|
|
|
299 |
list($key, $val) = explode($namedConfig['separator'], $param, 2);
|
|
|
300 |
$key = rawurldecode($key);
|
|
|
301 |
$val = rawurldecode($val);
|
|
|
302 |
$hasRule = isset($rules[$key]);
|
|
|
303 |
$passIt = (!$hasRule && !$greedy) || ($hasRule && !$this->_matchNamed($val, $rules[$key], $context));
|
|
|
304 |
if ($passIt) {
|
|
|
305 |
$pass[] = rawurldecode($param);
|
|
|
306 |
} else {
|
|
|
307 |
if (preg_match_all('/\[([A-Za-z0-9_-]+)?\]/', $key, $matches, PREG_SET_ORDER)) {
|
|
|
308 |
$matches = array_reverse($matches);
|
|
|
309 |
$parts = explode('[', $key);
|
|
|
310 |
$key = array_shift($parts);
|
|
|
311 |
$arr = $val;
|
|
|
312 |
foreach ($matches as $match) {
|
|
|
313 |
if (empty($match[1])) {
|
|
|
314 |
$arr = array($arr);
|
|
|
315 |
} else {
|
|
|
316 |
$arr = array(
|
|
|
317 |
$match[1] => $arr
|
|
|
318 |
);
|
|
|
319 |
}
|
|
|
320 |
}
|
|
|
321 |
$val = $arr;
|
|
|
322 |
}
|
|
|
323 |
$named = array_merge_recursive($named, array($key => $val));
|
|
|
324 |
}
|
|
|
325 |
} else {
|
|
|
326 |
$pass[] = rawurldecode($param);
|
|
|
327 |
}
|
|
|
328 |
}
|
|
|
329 |
return array($pass, $named);
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
/**
|
|
|
333 |
* Check if a named parameter matches the current rules.
|
|
|
334 |
*
|
|
|
335 |
* Return true if a given named $param's $val matches a given $rule depending on $context.
|
|
|
336 |
* Currently implemented rule types are controller, action and match that can be combined with each other.
|
|
|
337 |
*
|
|
|
338 |
* @param string $val The value of the named parameter
|
|
|
339 |
* @param array $rule The rule(s) to apply, can also be a match string
|
|
|
340 |
* @param string $context An array with additional context information (controller / action)
|
|
|
341 |
* @return boolean
|
|
|
342 |
*/
|
|
|
343 |
protected function _matchNamed($val, $rule, $context) {
|
|
|
344 |
if ($rule === true || $rule === false) {
|
|
|
345 |
return $rule;
|
|
|
346 |
}
|
|
|
347 |
if (is_string($rule)) {
|
|
|
348 |
$rule = array('match' => $rule);
|
|
|
349 |
}
|
|
|
350 |
if (!is_array($rule)) {
|
|
|
351 |
return false;
|
|
|
352 |
}
|
|
|
353 |
|
|
|
354 |
$controllerMatches = (
|
|
|
355 |
!isset($rule['controller'], $context['controller']) ||
|
|
|
356 |
in_array($context['controller'], (array)$rule['controller'])
|
|
|
357 |
);
|
|
|
358 |
if (!$controllerMatches) {
|
|
|
359 |
return false;
|
|
|
360 |
}
|
|
|
361 |
$actionMatches = (
|
|
|
362 |
!isset($rule['action'], $context['action']) ||
|
|
|
363 |
in_array($context['action'], (array)$rule['action'])
|
|
|
364 |
);
|
|
|
365 |
if (!$actionMatches) {
|
|
|
366 |
return false;
|
|
|
367 |
}
|
|
|
368 |
return (!isset($rule['match']) || preg_match('/' . $rule['match'] . '/', $val));
|
|
|
369 |
}
|
|
|
370 |
|
|
|
371 |
/**
|
|
|
372 |
* Apply persistent parameters to a URL array. Persistent parameters are a special
|
|
|
373 |
* key used during route creation to force route parameters to persist when omitted from
|
|
|
374 |
* a URL array.
|
|
|
375 |
*
|
|
|
376 |
* @param array $url The array to apply persistent parameters to.
|
|
|
377 |
* @param array $params An array of persistent values to replace persistent ones.
|
|
|
378 |
* @return array An array with persistent parameters applied.
|
|
|
379 |
*/
|
|
|
380 |
public function persistParams($url, $params) {
|
|
|
381 |
if (empty($this->options['persist']) || !is_array($this->options['persist'])) {
|
|
|
382 |
return $url;
|
|
|
383 |
}
|
|
|
384 |
foreach ($this->options['persist'] as $persistKey) {
|
|
|
385 |
if (array_key_exists($persistKey, $params) && !isset($url[$persistKey])) {
|
|
|
386 |
$url[$persistKey] = $params[$persistKey];
|
|
|
387 |
}
|
|
|
388 |
}
|
|
|
389 |
return $url;
|
|
|
390 |
}
|
|
|
391 |
|
|
|
392 |
/**
|
|
|
393 |
* Check if a URL array matches this route instance.
|
|
|
394 |
*
|
|
|
395 |
* If the URL matches the route parameters and settings, then
|
|
|
396 |
* return a generated string URL. If the URL doesn't match the route parameters, false will be returned.
|
|
|
397 |
* This method handles the reverse routing or conversion of URL arrays into string URLs.
|
|
|
398 |
*
|
|
|
399 |
* @param array $url An array of parameters to check matching with.
|
|
|
400 |
* @return mixed Either a string URL for the parameters if they match or false.
|
|
|
401 |
*/
|
|
|
402 |
public function match($url) {
|
|
|
403 |
if (!$this->compiled()) {
|
|
|
404 |
$this->compile();
|
|
|
405 |
}
|
|
|
406 |
$defaults = $this->defaults;
|
|
|
407 |
|
|
|
408 |
if (isset($defaults['prefix'])) {
|
|
|
409 |
$url['prefix'] = $defaults['prefix'];
|
|
|
410 |
}
|
|
|
411 |
|
|
|
412 |
//check that all the key names are in the url
|
|
|
413 |
$keyNames = array_flip($this->keys);
|
|
|
414 |
if (array_intersect_key($keyNames, $url) !== $keyNames) {
|
|
|
415 |
return false;
|
|
|
416 |
}
|
|
|
417 |
|
|
|
418 |
// Missing defaults is a fail.
|
|
|
419 |
if (array_diff_key($defaults, $url) !== array()) {
|
|
|
420 |
return false;
|
|
|
421 |
}
|
|
|
422 |
|
|
|
423 |
$namedConfig = Router::namedConfig();
|
|
|
424 |
$prefixes = Router::prefixes();
|
|
|
425 |
$greedyNamed = $namedConfig['greedyNamed'];
|
|
|
426 |
$allowedNamedParams = $namedConfig['rules'];
|
|
|
427 |
|
|
|
428 |
$named = $pass = array();
|
|
|
429 |
|
|
|
430 |
foreach ($url as $key => $value) {
|
|
|
431 |
|
|
|
432 |
// keys that exist in the defaults and have different values is a match failure.
|
|
|
433 |
$defaultExists = array_key_exists($key, $defaults);
|
|
|
434 |
if ($defaultExists && $defaults[$key] != $value) {
|
|
|
435 |
return false;
|
|
|
436 |
} elseif ($defaultExists) {
|
|
|
437 |
continue;
|
|
|
438 |
}
|
|
|
439 |
|
|
|
440 |
// If the key is a routed key, its not different yet.
|
|
|
441 |
if (array_key_exists($key, $keyNames)) {
|
|
|
442 |
continue;
|
|
|
443 |
}
|
|
|
444 |
|
|
|
445 |
// pull out passed args
|
|
|
446 |
$numeric = is_numeric($key);
|
|
|
447 |
if ($numeric && isset($defaults[$key]) && $defaults[$key] == $value) {
|
|
|
448 |
continue;
|
|
|
449 |
} elseif ($numeric) {
|
|
|
450 |
$pass[] = $value;
|
|
|
451 |
unset($url[$key]);
|
|
|
452 |
continue;
|
|
|
453 |
}
|
|
|
454 |
|
|
|
455 |
// pull out named params if named params are greedy or a rule exists.
|
|
|
456 |
if (
|
|
|
457 |
($greedyNamed || isset($allowedNamedParams[$key])) &&
|
|
|
458 |
($value !== false && $value !== null) &&
|
|
|
459 |
(!in_array($key, $prefixes))
|
|
|
460 |
) {
|
|
|
461 |
$named[$key] = $value;
|
|
|
462 |
continue;
|
|
|
463 |
}
|
|
|
464 |
|
|
|
465 |
// keys that don't exist are different.
|
|
|
466 |
if (!$defaultExists && !empty($value)) {
|
|
|
467 |
return false;
|
|
|
468 |
}
|
|
|
469 |
}
|
|
|
470 |
|
|
|
471 |
//if a not a greedy route, no extra params are allowed.
|
|
|
472 |
if (!$this->_greedy && (!empty($pass) || !empty($named))) {
|
|
|
473 |
return false;
|
|
|
474 |
}
|
|
|
475 |
|
|
|
476 |
//check patterns for routed params
|
|
|
477 |
if (!empty($this->options)) {
|
|
|
478 |
foreach ($this->options as $key => $pattern) {
|
|
|
479 |
if (array_key_exists($key, $url) && !preg_match('#^' . $pattern . '$#', $url[$key])) {
|
|
|
480 |
return false;
|
|
|
481 |
}
|
|
|
482 |
}
|
|
|
483 |
}
|
|
|
484 |
return $this->_writeUrl(array_merge($url, compact('pass', 'named')));
|
|
|
485 |
}
|
|
|
486 |
|
|
|
487 |
/**
|
|
|
488 |
* Converts a matching route array into a URL string.
|
|
|
489 |
*
|
|
|
490 |
* Composes the string URL using the template
|
|
|
491 |
* used to create the route.
|
|
|
492 |
*
|
|
|
493 |
* @param array $params The params to convert to a string URL.
|
|
|
494 |
* @return string Composed route string.
|
|
|
495 |
*/
|
|
|
496 |
protected function _writeUrl($params) {
|
|
|
497 |
if (isset($params['prefix'])) {
|
|
|
498 |
$prefixed = $params['prefix'] . '_';
|
|
|
499 |
}
|
|
|
500 |
if (isset($prefixed, $params['action']) && strpos($params['action'], $prefixed) === 0) {
|
|
|
501 |
$params['action'] = substr($params['action'], strlen($prefixed) * -1);
|
|
|
502 |
unset($params['prefix']);
|
|
|
503 |
}
|
|
|
504 |
|
|
|
505 |
if (is_array($params['pass'])) {
|
|
|
506 |
$params['pass'] = implode('/', array_map('rawurlencode', $params['pass']));
|
|
|
507 |
}
|
|
|
508 |
|
|
|
509 |
$namedConfig = Router::namedConfig();
|
|
|
510 |
$separator = $namedConfig['separator'];
|
|
|
511 |
|
|
|
512 |
if (!empty($params['named']) && is_array($params['named'])) {
|
|
|
513 |
$named = array();
|
|
|
514 |
foreach ($params['named'] as $key => $value) {
|
|
|
515 |
if (is_array($value)) {
|
|
|
516 |
$flat = Hash::flatten($value, '%5D%5B');
|
|
|
517 |
foreach ($flat as $namedKey => $namedValue) {
|
|
|
518 |
$named[] = $key . "%5B{$namedKey}%5D" . $separator . rawurlencode($namedValue);
|
|
|
519 |
}
|
|
|
520 |
} else {
|
|
|
521 |
$named[] = $key . $separator . rawurlencode($value);
|
|
|
522 |
}
|
|
|
523 |
}
|
|
|
524 |
$params['pass'] = $params['pass'] . '/' . implode('/', $named);
|
|
|
525 |
}
|
|
|
526 |
$out = $this->template;
|
|
|
527 |
|
|
|
528 |
$search = $replace = array();
|
|
|
529 |
foreach ($this->keys as $key) {
|
|
|
530 |
$string = null;
|
|
|
531 |
if (isset($params[$key])) {
|
|
|
532 |
$string = $params[$key];
|
|
|
533 |
} elseif (strpos($out, $key) != strlen($out) - strlen($key)) {
|
|
|
534 |
$key .= '/';
|
|
|
535 |
}
|
|
|
536 |
$search[] = ':' . $key;
|
|
|
537 |
$replace[] = $string;
|
|
|
538 |
}
|
|
|
539 |
$out = str_replace($search, $replace, $out);
|
|
|
540 |
|
|
|
541 |
if (strpos($this->template, '*')) {
|
|
|
542 |
$out = str_replace('*', $params['pass'], $out);
|
|
|
543 |
}
|
|
|
544 |
$out = str_replace('//', '/', $out);
|
|
|
545 |
return $out;
|
|
|
546 |
}
|
|
|
547 |
|
|
|
548 |
}
|