|
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\Exception; |
|
12
|
|
|
use yii\base\InlineAction; |
|
13
|
|
|
use yii\helpers\Url; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Controller is the base class of web controllers. |
|
17
|
|
|
* |
|
18
|
|
|
* For more details and usage information on Controller, see the [guide article on controllers](guide:structure-controllers). |
|
19
|
|
|
* |
|
20
|
|
|
* @author Qiang Xue <[email protected]> |
|
21
|
|
|
* @since 2.0 |
|
22
|
|
|
*/ |
|
23
|
|
|
class Controller extends \yii\base\Controller |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var bool whether to enable CSRF validation for the actions in this controller. |
|
27
|
|
|
* CSRF validation is enabled only when both this property and [[\yii\web\Request::enableCsrfValidation]] are true. |
|
28
|
|
|
*/ |
|
29
|
|
|
public $enableCsrfValidation = true; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var array the parameters bound to the current action. |
|
32
|
|
|
*/ |
|
33
|
|
|
public $actionParams = []; |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Renders a view in response to an AJAX request. |
|
38
|
|
|
* |
|
39
|
|
|
* This method is similar to [[renderPartial()]] except that it will inject into |
|
40
|
|
|
* the rendering result with JS/CSS scripts and files which are registered with the view. |
|
41
|
|
|
* For this reason, you should use this method instead of [[renderPartial()]] to render |
|
42
|
|
|
* a view to respond to an AJAX request. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $view the view name. Please refer to [[render()]] on how to specify a view name. |
|
45
|
|
|
* @param array $params the parameters (name-value pairs) that should be made available in the view. |
|
46
|
|
|
* @return string the rendering result. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function renderAjax($view, $params = []) |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->getView()->renderAjax($view, $params, $this); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Send data formatted as JSON. |
|
55
|
|
|
* |
|
56
|
|
|
* This method is a shortcut for sending data formatted as JSON. It will return |
|
57
|
|
|
* the [[Application::getResponse()|response]] application component after configuring |
|
58
|
|
|
* the [[Response::$format|format]] and setting the [[Response::$data|data]] that should |
|
59
|
|
|
* be formatted. A common usage will be: |
|
60
|
|
|
* |
|
61
|
|
|
* ```php |
|
62
|
|
|
* return $this->asJson($data); |
|
63
|
|
|
* ``` |
|
64
|
|
|
* |
|
65
|
|
|
* @param mixed $data the data that should be formatted. |
|
66
|
|
|
* @return Response a response that is configured to send `$data` formatted as JSON. |
|
67
|
|
|
* @since 2.0.11 |
|
68
|
|
|
* @see Response::$format |
|
69
|
|
|
* @see Response::FORMAT_JSON |
|
70
|
|
|
* @see JsonResponseFormatter |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function asJson($data) |
|
73
|
|
|
{ |
|
74
|
1 |
|
$this->response->format = Response::FORMAT_JSON; |
|
|
|
|
|
|
75
|
1 |
|
$this->response->data = $data; |
|
|
|
|
|
|
76
|
1 |
|
return $this->response; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Send data formatted as XML. |
|
81
|
|
|
* |
|
82
|
|
|
* This method is a shortcut for sending data formatted as XML. It will return |
|
83
|
|
|
* the [[Application::getResponse()|response]] application component after configuring |
|
84
|
|
|
* the [[Response::$format|format]] and setting the [[Response::$data|data]] that should |
|
85
|
|
|
* be formatted. A common usage will be: |
|
86
|
|
|
* |
|
87
|
|
|
* ```php |
|
88
|
|
|
* return $this->asXml($data); |
|
89
|
|
|
* ``` |
|
90
|
|
|
* |
|
91
|
|
|
* @param mixed $data the data that should be formatted. |
|
92
|
|
|
* @return Response a response that is configured to send `$data` formatted as XML. |
|
93
|
|
|
* @since 2.0.11 |
|
94
|
|
|
* @see Response::$format |
|
95
|
|
|
* @see Response::FORMAT_XML |
|
96
|
|
|
* @see XmlResponseFormatter |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function asXml($data) |
|
99
|
|
|
{ |
|
100
|
1 |
|
$this->response->format = Response::FORMAT_XML; |
|
|
|
|
|
|
101
|
1 |
|
$this->response->data = $data; |
|
|
|
|
|
|
102
|
1 |
|
return $this->response; |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Binds the parameters to the action. |
|
107
|
|
|
* This method is invoked by [[\yii\base\Action]] when it begins to run with the given parameters. |
|
108
|
|
|
* This method will check the parameter names that the action requires and return |
|
109
|
|
|
* the provided parameters according to the requirement. If there is any missing parameter, |
|
110
|
|
|
* an exception will be thrown. |
|
111
|
|
|
* @param \yii\base\Action $action the action to be bound with parameters |
|
112
|
|
|
* @param array $params the parameters to be bound to the action |
|
113
|
|
|
* @return array the valid parameters that the action can run with. |
|
114
|
|
|
* @throws BadRequestHttpException if there are missing or invalid parameters. |
|
115
|
|
|
*/ |
|
116
|
91 |
|
public function bindActionParams($action, $params) |
|
117
|
|
|
{ |
|
118
|
91 |
|
if ($action instanceof InlineAction) { |
|
119
|
77 |
|
$method = new \ReflectionMethod($this, $action->actionMethod); |
|
120
|
|
|
} else { |
|
121
|
14 |
|
$method = new \ReflectionMethod($action, 'run'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
91 |
|
$args = []; |
|
125
|
91 |
|
$missing = []; |
|
126
|
91 |
|
$actionParams = []; |
|
127
|
91 |
|
$requestedParams = []; |
|
128
|
91 |
|
foreach ($method->getParameters() as $param) { |
|
129
|
9 |
|
$name = $param->getName(); |
|
130
|
9 |
|
if (array_key_exists($name, $params)) { |
|
131
|
6 |
|
$isValid = true; |
|
132
|
6 |
|
$isArray = ($type = $param->getType()) instanceof \ReflectionNamedType && $type->getName() === 'array'; |
|
133
|
6 |
|
if ($isArray) { |
|
134
|
|
|
$params[$name] = (array)$params[$name]; |
|
135
|
6 |
|
} elseif (is_array($params[$name])) { |
|
136
|
|
|
$isValid = false; |
|
137
|
|
|
} elseif ( |
|
138
|
6 |
|
PHP_VERSION_ID >= 70000 |
|
139
|
6 |
|
&& ($type = $param->getType()) !== null |
|
140
|
6 |
|
&& method_exists($type, 'isBuiltin') |
|
141
|
6 |
|
&& $type->isBuiltin() |
|
142
|
6 |
|
&& ($params[$name] !== null || !$type->allowsNull()) |
|
143
|
|
|
) { |
|
144
|
1 |
|
$typeName = PHP_VERSION_ID >= 70100 ? $type->getName() : (string)$type; |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
1 |
|
if ($params[$name] === '' && $type->allowsNull()) { |
|
147
|
1 |
|
if ($typeName !== 'string') { // for old string behavior compatibility |
|
148
|
1 |
|
$params[$name] = null; |
|
149
|
|
|
} |
|
150
|
|
|
} else { |
|
151
|
|
|
switch ($typeName) { |
|
152
|
1 |
|
case 'int': |
|
153
|
1 |
|
$params[$name] = filter_var($params[$name], FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE); |
|
154
|
1 |
|
break; |
|
155
|
1 |
|
case 'float': |
|
156
|
|
|
$params[$name] = filter_var($params[$name], FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE); |
|
157
|
|
|
break; |
|
158
|
1 |
|
case 'bool': |
|
159
|
1 |
|
$params[$name] = filter_var($params[$name], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); |
|
160
|
1 |
|
break; |
|
161
|
|
|
} |
|
162
|
1 |
|
if ($params[$name] === null) { |
|
163
|
1 |
|
$isValid = false; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
6 |
|
if (!$isValid) { |
|
168
|
1 |
|
throw new BadRequestHttpException( |
|
169
|
1 |
|
Yii::t('yii', 'Invalid data received for parameter "{param}".', ['param' => $name]) |
|
170
|
1 |
|
); |
|
171
|
|
|
} |
|
172
|
6 |
|
$args[] = $actionParams[$name] = $params[$name]; |
|
173
|
6 |
|
unset($params[$name]); |
|
174
|
|
|
} elseif ( |
|
175
|
7 |
|
PHP_VERSION_ID >= 70100 |
|
176
|
7 |
|
&& ($type = $param->getType()) !== null |
|
177
|
7 |
|
&& $type instanceof \ReflectionNamedType |
|
178
|
7 |
|
&& !$type->isBuiltin() |
|
179
|
|
|
) { |
|
180
|
|
|
try { |
|
181
|
6 |
|
$this->bindInjectedParams($type, $name, $args, $requestedParams); |
|
182
|
3 |
|
} catch (HttpException $e) { |
|
183
|
1 |
|
throw $e; |
|
184
|
2 |
|
} catch (Exception $e) { |
|
185
|
5 |
|
throw new ServerErrorHttpException($e->getMessage(), 0, $e); |
|
186
|
|
|
} |
|
187
|
1 |
|
} elseif ($param->isDefaultValueAvailable()) { |
|
188
|
1 |
|
$args[] = $actionParams[$name] = $param->getDefaultValue(); |
|
189
|
|
|
} else { |
|
190
|
|
|
$missing[] = $name; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
88 |
|
if (!empty($missing)) { |
|
195
|
|
|
throw new BadRequestHttpException( |
|
196
|
|
|
Yii::t('yii', 'Missing required parameters: {params}', ['params' => implode(', ', $missing)]) |
|
197
|
|
|
); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
88 |
|
$this->actionParams = $actionParams; |
|
201
|
|
|
|
|
202
|
|
|
// We use a different array here, specifically one that doesn't contain service instances but descriptions instead. |
|
203
|
88 |
|
if (Yii::$app->requestedParams === null) { |
|
204
|
88 |
|
Yii::$app->requestedParams = array_merge($actionParams, $requestedParams); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
88 |
|
return $args; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* {@inheritdoc} |
|
212
|
|
|
*/ |
|
213
|
83 |
|
public function beforeAction($action) |
|
214
|
|
|
{ |
|
215
|
83 |
|
if (parent::beforeAction($action)) { |
|
216
|
77 |
|
if ($this->enableCsrfValidation && Yii::$app->getErrorHandler()->exception === null && !$this->request->validateCsrfToken()) { |
|
|
|
|
|
|
217
|
|
|
throw new BadRequestHttpException(Yii::t('yii', 'Unable to verify your data submission.')); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
77 |
|
return true; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
return false; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Redirects the browser to the specified URL. |
|
228
|
|
|
* This method is a shortcut to [[Response::redirect()]]. |
|
229
|
|
|
* |
|
230
|
|
|
* You can use it in an action by returning the [[Response]] directly: |
|
231
|
|
|
* |
|
232
|
|
|
* ```php |
|
233
|
|
|
* // stop executing this action and redirect to login page |
|
234
|
|
|
* return $this->redirect(['login']); |
|
235
|
|
|
* ``` |
|
236
|
|
|
* |
|
237
|
|
|
* @param string|array $url the URL to be redirected to. This can be in one of the following formats: |
|
238
|
|
|
* |
|
239
|
|
|
* - a string representing a URL (e.g. "https://example.com") |
|
240
|
|
|
* - a string representing a URL alias (e.g. "@example.com") |
|
241
|
|
|
* - an array in the format of `[$route, ...name-value pairs...]` (e.g. `['site/index', 'ref' => 1]`) |
|
242
|
|
|
* [[Url::to()]] will be used to convert the array into a URL. |
|
243
|
|
|
* |
|
244
|
|
|
* Any relative URL that starts with a single forward slash "/" will be converted |
|
245
|
|
|
* into an absolute one by prepending it with the host info of the current request. |
|
246
|
|
|
* |
|
247
|
|
|
* @param int $statusCode the HTTP status code. Defaults to 302. |
|
248
|
|
|
* See <https://tools.ietf.org/html/rfc2616#section-10> |
|
249
|
|
|
* for details about HTTP status code |
|
250
|
|
|
* @return Response the current response object |
|
251
|
|
|
*/ |
|
252
|
1 |
|
public function redirect($url, $statusCode = 302) |
|
253
|
|
|
{ |
|
254
|
|
|
// calling Url::to() here because Response::redirect() modifies route before calling Url::to() |
|
255
|
1 |
|
return $this->response->redirect(Url::to($url), $statusCode); |
|
|
|
|
|
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Redirects the browser to the home page. |
|
260
|
|
|
* |
|
261
|
|
|
* You can use this method in an action by returning the [[Response]] directly: |
|
262
|
|
|
* |
|
263
|
|
|
* ```php |
|
264
|
|
|
* // stop executing this action and redirect to home page |
|
265
|
|
|
* return $this->goHome(); |
|
266
|
|
|
* ``` |
|
267
|
|
|
* |
|
268
|
|
|
* @return Response the current response object |
|
269
|
|
|
*/ |
|
270
|
|
|
public function goHome() |
|
271
|
|
|
{ |
|
272
|
|
|
return $this->response->redirect(Yii::$app->getHomeUrl()); |
|
|
|
|
|
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Redirects the browser to the last visited page. |
|
277
|
|
|
* |
|
278
|
|
|
* You can use this method in an action by returning the [[Response]] directly: |
|
279
|
|
|
* |
|
280
|
|
|
* ```php |
|
281
|
|
|
* // stop executing this action and redirect to last visited page |
|
282
|
|
|
* return $this->goBack(); |
|
283
|
|
|
* ``` |
|
284
|
|
|
* |
|
285
|
|
|
* For this function to work you have to [[User::setReturnUrl()|set the return URL]] in appropriate places before. |
|
286
|
|
|
* |
|
287
|
|
|
* @param string|array|null $defaultUrl the default return URL in case it was not set previously. |
|
288
|
|
|
* If this is null and the return URL was not set previously, [[Application::homeUrl]] will be redirected to. |
|
289
|
|
|
* Please refer to [[User::setReturnUrl()]] on accepted format of the URL. |
|
290
|
|
|
* @return Response the current response object |
|
291
|
|
|
* @see User::getReturnUrl() |
|
292
|
|
|
*/ |
|
293
|
|
|
public function goBack($defaultUrl = null) |
|
294
|
|
|
{ |
|
295
|
|
|
return $this->response->redirect(Yii::$app->getUser()->getReturnUrl($defaultUrl)); |
|
|
|
|
|
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Refreshes the current page. |
|
300
|
|
|
* This method is a shortcut to [[Response::refresh()]]. |
|
301
|
|
|
* |
|
302
|
|
|
* You can use it in an action by returning the [[Response]] directly: |
|
303
|
|
|
* |
|
304
|
|
|
* ```php |
|
305
|
|
|
* // stop executing this action and refresh the current page |
|
306
|
|
|
* return $this->refresh(); |
|
307
|
|
|
* ``` |
|
308
|
|
|
* |
|
309
|
|
|
* @param string $anchor the anchor that should be appended to the redirection URL. |
|
310
|
|
|
* Defaults to empty. Make sure the anchor starts with '#' if you want to specify it. |
|
311
|
|
|
* @return Response the response object itself |
|
312
|
|
|
*/ |
|
313
|
|
|
public function refresh($anchor = '') |
|
314
|
|
|
{ |
|
315
|
|
|
return $this->response->redirect($this->request->getUrl() . $anchor); |
|
|
|
|
|
|
316
|
|
|
} |
|
317
|
|
|
} |
|
318
|
|
|
|