1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license https://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\web; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* GroupUrlRule represents a collection of URL rules sharing the same prefix in their patterns and routes. |
15
|
|
|
* |
16
|
|
|
* GroupUrlRule is best used by a module which often uses module ID as the prefix for the URL rules. |
17
|
|
|
* For example, the following code creates a rule for the `admin` module: |
18
|
|
|
* |
19
|
|
|
* ```php |
20
|
|
|
* new GroupUrlRule([ |
21
|
|
|
* 'prefix' => 'admin', |
22
|
|
|
* 'rules' => [ |
23
|
|
|
* 'login' => 'user/login', |
24
|
|
|
* 'logout' => 'user/logout', |
25
|
|
|
* 'dashboard' => 'default/dashboard', |
26
|
|
|
* ], |
27
|
|
|
* ]); |
28
|
|
|
* |
29
|
|
|
* // the above rule is equivalent to the following three rules: |
30
|
|
|
* |
31
|
|
|
* [ |
32
|
|
|
* 'admin/login' => 'admin/user/login', |
33
|
|
|
* 'admin/logout' => 'admin/user/logout', |
34
|
|
|
* 'admin/dashboard' => 'admin/default/dashboard', |
35
|
|
|
* ] |
36
|
|
|
* ``` |
37
|
|
|
* |
38
|
|
|
* The above example assumes the prefix for patterns and routes are the same. They can be made different |
39
|
|
|
* by configuring [[prefix]] and [[routePrefix]] separately. |
40
|
|
|
* |
41
|
|
|
* Using a GroupUrlRule is more efficient than directly declaring the individual rules it contains. |
42
|
|
|
* This is because GroupUrlRule can quickly determine if it should process a URL parsing or creation request |
43
|
|
|
* by simply checking if the prefix matches. |
44
|
|
|
* |
45
|
|
|
* @author Qiang Xue <[email protected]> |
46
|
|
|
* @since 2.0 |
47
|
|
|
*/ |
48
|
|
|
class GroupUrlRule extends CompositeUrlRule |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* @var array the rules contained within this composite rule. Please refer to [[UrlManager::rules]] |
52
|
|
|
* for the format of this property. |
53
|
|
|
* @see prefix |
54
|
|
|
* @see routePrefix |
55
|
|
|
*/ |
56
|
|
|
public $rules = []; |
57
|
|
|
/** |
58
|
|
|
* @var string the prefix for the pattern part of every rule declared in [[rules]]. |
59
|
|
|
* The prefix and the pattern will be separated with a slash. |
60
|
|
|
*/ |
61
|
|
|
public $prefix; |
62
|
|
|
/** |
63
|
|
|
* @var string|null the prefix for the route part of every rule declared in [[rules]]. |
64
|
|
|
* The prefix and the route will be separated with a slash. |
65
|
|
|
* If this property is not set, it will take the value of [[prefix]]. |
66
|
|
|
*/ |
67
|
|
|
public $routePrefix; |
68
|
|
|
/** |
69
|
|
|
* @var array the default configuration of URL rules. Individual rule configurations |
70
|
|
|
* specified via [[rules]] will take precedence when the same property of the rule is configured. |
71
|
|
|
*/ |
72
|
|
|
public $ruleConfig = ['class' => 'yii\web\UrlRule']; |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
3 |
|
public function init() |
79
|
|
|
{ |
80
|
3 |
|
$this->prefix = trim((string)$this->prefix, '/'); |
81
|
3 |
|
$this->routePrefix = $this->routePrefix === null ? $this->prefix : trim($this->routePrefix, '/'); |
82
|
3 |
|
parent::init(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
3 |
|
protected function createRules() |
89
|
|
|
{ |
90
|
3 |
|
$rules = []; |
91
|
3 |
|
foreach ($this->rules as $key => $rule) { |
92
|
3 |
|
if (!is_array($rule)) { |
93
|
3 |
|
$verbs = 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS'; |
94
|
3 |
|
$verb = null; |
95
|
3 |
|
if (preg_match("/^((?:(?:$verbs),)*(?:$verbs))\\s+(.*)$/", $key, $matches)) { |
96
|
1 |
|
$verb = explode(',', $matches[1]); |
97
|
1 |
|
$key = $matches[2]; |
98
|
|
|
} |
99
|
3 |
|
$rule = [ |
100
|
3 |
|
'pattern' => ltrim($this->prefix . '/' . $key, '/'), |
101
|
3 |
|
'route' => ltrim($this->routePrefix . '/' . $rule, '/'), |
102
|
3 |
|
'verb' => $verb |
103
|
3 |
|
]; |
104
|
2 |
|
} elseif (isset($rule['pattern'], $rule['route'])) { |
105
|
2 |
|
$rule['pattern'] = ltrim($this->prefix . '/' . $rule['pattern'], '/'); |
106
|
2 |
|
$rule['route'] = ltrim($this->routePrefix . '/' . $rule['route'], '/'); |
107
|
|
|
} |
108
|
|
|
|
109
|
3 |
|
$rule = Yii::createObject(array_merge($this->ruleConfig, $rule)); |
110
|
3 |
|
if (!$rule instanceof UrlRuleInterface) { |
111
|
|
|
throw new InvalidConfigException('URL rule class must implement UrlRuleInterface.'); |
112
|
|
|
} |
113
|
3 |
|
$rules[] = $rule; |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
return $rules; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
1 |
|
public function parseRequest($manager, $request) |
123
|
|
|
{ |
124
|
1 |
|
$pathInfo = $request->getPathInfo(); |
125
|
1 |
|
if ($this->prefix === '' || strpos($pathInfo . '/', $this->prefix . '/') === 0) { |
126
|
1 |
|
return parent::parseRequest($manager, $request); |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
1 |
|
public function createUrl($manager, $route, $params) |
136
|
|
|
{ |
137
|
1 |
|
if ($this->routePrefix === '' || strpos($route, $this->routePrefix . '/') === 0) { |
138
|
1 |
|
return parent::createUrl($manager, $route, $params); |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
$this->createStatus = UrlRule::CREATE_STATUS_ROUTE_MISMATCH; |
142
|
1 |
|
return false; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|