1
|
|
|
<?php |
2
|
|
|
namespace App\Routing\Route; |
3
|
|
|
|
4
|
|
|
use Cake\Routing\Route\Route; |
5
|
|
|
use Cake\Utility\Inflector; |
6
|
|
|
|
7
|
|
|
class SlugRoute extends Route |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Flag for tracking whether or not the defaults have been sluged. |
12
|
|
|
* |
13
|
|
|
* Default values need to be sluged so that they match the inflections that |
14
|
|
|
* match() will create. |
15
|
|
|
* |
16
|
|
|
* @var bool |
17
|
|
|
*/ |
18
|
|
|
protected $_slugedDefaults = false; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Parses a string URL into an array. If it matches, it will convert the slug keys to their slugerize form |
22
|
|
|
* |
23
|
|
|
* @param string $url The URL to parse. |
24
|
|
|
* @param string $method The HTTP method. |
25
|
|
|
* |
26
|
|
|
* @return false|array False on failure, or an array of request parameters. |
27
|
|
|
*/ |
28
|
|
|
public function parse($url, $method = '') |
29
|
|
|
{ |
30
|
|
|
$params = parent::parse($url, $method); |
|
|
|
|
31
|
|
|
if (!$params) { |
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
if (!empty($params['slug'])) { |
35
|
|
|
$params['slug'] = strtolower(Inflector::slug($params['slug'])); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
if (!empty($params['controller'])) { |
38
|
|
|
$params['controller'] = Inflector::camelize($params['controller']); |
39
|
|
|
} |
40
|
|
|
if (!empty($params['plugin'])) { |
41
|
|
|
$params['plugin'] = Inflector::camelize($params['plugin']); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $params; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Slug the prefix, controller and plugin params before passing them on to the |
49
|
|
|
* parent class |
50
|
|
|
* |
51
|
|
|
* @param array $url Array of parameters to convert to a string. |
52
|
|
|
* @param array $context An array of the current request context. |
53
|
|
|
* Contains information such as the current host, scheme, port, and base |
54
|
|
|
* directory. |
55
|
|
|
* |
56
|
|
|
* @return false|string Either false or a string URL. |
57
|
|
|
*/ |
58
|
|
|
public function match(array $url, array $context = []) |
59
|
|
|
{ |
60
|
|
|
$url = $this->_slugerize($url); |
61
|
|
|
if (!$this->_slugedDefaults) { |
62
|
|
|
$this->_slugedDefaults = true; |
63
|
|
|
$this->defaults = $this->_slugerize($this->defaults); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return parent::match($url, $context); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Helper method for slugerizing keys in a URL array. |
71
|
|
|
* |
72
|
|
|
* @param array $url An array of URL keys. |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
protected function _slugerize($url) |
77
|
|
|
{ |
78
|
|
|
if (isset($url['slug']) && !empty($url['slug'])) { |
79
|
|
|
$url['slug'] = strtolower(Inflector::slug($url['slug'])); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
if (!empty($url['controller'])) { |
82
|
|
|
$url['controller'] = Inflector::underscore($url['controller']); |
83
|
|
|
} |
84
|
|
|
if (!empty($url['plugin'])) { |
85
|
|
|
$url['plugin'] = Inflector::underscore($url['plugin']); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $url; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.