|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
-
-
- namespace yii\web;
-
- use Yii;
- use yii\base\InlineAction;
- use yii\helpers\Url;
-
-
- class Controller extends \yii\base\Controller
- {
-
-
- public $enableCsrfValidation = true;
-
-
- public $actionParams = [];
-
-
-
-
- public function renderAjax($view, $params = [])
- {
- return $this->getView()->renderAjax($view, $params, $this);
- }
-
-
-
- public function bindActionParams($action, $params)
- {
- if ($action instanceof InlineAction) {
- $method = new \ReflectionMethod($this, $action->actionMethod);
- } else {
- $method = new \ReflectionMethod($action, 'run');
- }
-
- $args = [];
- $missing = [];
- $actionParams = [];
- foreach ($method->getParameters() as $param) {
- $name = $param->getName();
- if (array_key_exists($name, $params)) {
- if ($param->isArray()) {
- $args[] = $actionParams[$name] = (array) $params[$name];
- } elseif (!is_array($params[$name])) {
- $args[] = $actionParams[$name] = $params[$name];
- } else {
- throw new BadRequestHttpException(Yii::t('yii', 'Invalid data received for parameter "{param}".', [
- 'param' => $name,
- ]));
- }
- unset($params[$name]);
- } elseif ($param->isDefaultValueAvailable()) {
- $args[] = $actionParams[$name] = $param->getDefaultValue();
- } else {
- $missing[] = $name;
- }
- }
-
- if (!empty($missing)) {
- throw new BadRequestHttpException(Yii::t('yii', 'Missing required parameters: {params}', [
- 'params' => implode(', ', $missing),
- ]));
- }
-
- $this->actionParams = $actionParams;
-
- return $args;
- }
-
-
-
- public function beforeAction($action)
- {
- if (parent::beforeAction($action)) {
- if ($this->enableCsrfValidation && Yii::$app->getErrorHandler()->exception === null && !Yii::$app->getRequest()->validateCsrfToken()) {
- throw new BadRequestHttpException(Yii::t('yii', 'Unable to verify your data submission.'));
- }
- return true;
- }
-
- return false;
- }
-
-
-
- public function redirect($url, $statusCode = 302)
- {
- return Yii::$app->getResponse()->redirect(Url::to($url), $statusCode);
- }
-
-
-
- public function goHome()
- {
- return Yii::$app->getResponse()->redirect(Yii::$app->getHomeUrl());
- }
-
-
-
- public function goBack($defaultUrl = null)
- {
- return Yii::$app->getResponse()->redirect(Yii::$app->getUser()->getReturnUrl($defaultUrl));
- }
-
-
-
- public function refresh($anchor = '')
- {
- return Yii::$app->getResponse()->redirect(Yii::$app->getRequest()->getUrl() . $anchor);
- }
- }
|