/
InflectedRoute.php
73 lines (68 loc) · 2.33 KB
/
InflectedRoute.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Routing\Route;
use Cake\Routing\Route\Route;
use Cake\Utility\Inflector;
/**
* This route class will transparently inflect the controller and plugin routing
* parameters, so that requesting `/my_controller` is parsed as `['controller' => 'MyController']`
*/
class InflectedRoute extends Route {
/**
* Parses a string URL into an array. If it mathes, it will convert the prefix, controller and
* plugin keys to their camelized form
*
* @param string $url The URL to parse
* @return mixed false on failure, or an array of request parameters
*/
public function parse($url) {
$params = parent::parse($url);
if (!$params) {
return false;
}
if (!empty($params['controller'])) {
$params['controller'] = Inflector::camelize($params['controller']);
}
if (!empty($params['plugin'])) {
$params['plugin'] = Inflector::camelize($params['plugin']);
}
if (!empty($params['prefix'])) {
$params['prefix'] = Inflector::camelize($params['prefix']);
}
return $params;
}
/**
* Underscores the prefix, controller and plugin params before passing them on to the
* parent class
*
* @param array $url Array of parameters to convert to a string.
* @param array $context An array of the current request context.
* Contains information such as the current host, scheme, port, and base
* directory.
* @return mixed either false or a string URL.
*/
public function match(array $url, array $context = array()) {
if (!empty($url['controller'])) {
$url['controller'] = Inflector::underscore($url['controller']);
}
if (!empty($url['plugin'])) {
$url['plugin'] = Inflector::underscore($url['plugin']);
}
if (!empty($url['prefix'])) {
$url['prefix'] = Inflector::underscore($url['prefix']);
}
return parent::match($url, $context);
}
}