123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- <?php
- namespace yii\web;
- use Yii;
- use yii\base\Exception;
- 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 asJson($data)
- {
- $this->response->format = Response::FORMAT_JSON;
- $this->response->data = $data;
- return $this->response;
- }
-
- public function asXml($data)
- {
- $this->response->format = Response::FORMAT_XML;
- $this->response->data = $data;
- return $this->response;
- }
-
- public function bindActionParams($action, $params)
- {
- if ($action instanceof InlineAction) {
- $method = new \ReflectionMethod($this, $action->actionMethod);
- } else {
- $method = new \ReflectionMethod($action, 'run');
- }
- $args = [];
- $missing = [];
- $actionParams = [];
- $requestedParams = [];
- foreach ($method->getParameters() as $param) {
- $name = $param->getName();
- if (array_key_exists($name, $params)) {
- $isValid = true;
- $isArray = ($type = $param->getType()) instanceof \ReflectionNamedType && $type->getName() === 'array';
- if ($isArray) {
- $params[$name] = (array)$params[$name];
- } elseif (is_array($params[$name])) {
- $isValid = false;
- } elseif (
- PHP_VERSION_ID >= 70000
- && ($type = $param->getType()) !== null
- && method_exists($type, 'isBuiltin')
- && $type->isBuiltin()
- && ($params[$name] !== null || !$type->allowsNull())
- ) {
- $typeName = PHP_VERSION_ID >= 70100 ? $type->getName() : (string)$type;
- if ($params[$name] === '' && $type->allowsNull()) {
- if ($typeName !== 'string') {
- $params[$name] = null;
- }
- } else {
- switch ($typeName) {
- case 'int':
- $params[$name] = filter_var($params[$name], FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
- break;
- case 'float':
- $params[$name] = filter_var($params[$name], FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE);
- break;
- case 'bool':
- $params[$name] = filter_var($params[$name], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
- break;
- }
- if ($params[$name] === null) {
- $isValid = false;
- }
- }
- }
- if (!$isValid) {
- throw new BadRequestHttpException(
- Yii::t('yii', 'Invalid data received for parameter "{param}".', ['param' => $name])
- );
- }
- $args[] = $actionParams[$name] = $params[$name];
- unset($params[$name]);
- } elseif (
- PHP_VERSION_ID >= 70100
- && ($type = $param->getType()) !== null
- && $type instanceof \ReflectionNamedType
- && !$type->isBuiltin()
- ) {
- try {
- $this->bindInjectedParams($type, $name, $args, $requestedParams);
- } catch (HttpException $e) {
- throw $e;
- } catch (Exception $e) {
- throw new ServerErrorHttpException($e->getMessage(), 0, $e);
- }
- } 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;
-
- if (Yii::$app->requestedParams === null) {
- Yii::$app->requestedParams = array_merge($actionParams, $requestedParams);
- }
- return $args;
- }
-
- public function beforeAction($action)
- {
- if (parent::beforeAction($action)) {
- if ($this->enableCsrfValidation && Yii::$app->getErrorHandler()->exception === null && !$this->request->validateCsrfToken()) {
- throw new BadRequestHttpException(Yii::t('yii', 'Unable to verify your data submission.'));
- }
- return true;
- }
- return false;
- }
-
- public function redirect($url, $statusCode = 302)
- {
-
- return $this->response->redirect(Url::to($url), $statusCode);
- }
-
- public function goHome()
- {
- return $this->response->redirect(Yii::$app->getHomeUrl());
- }
-
- public function goBack($defaultUrl = null)
- {
- return $this->response->redirect(Yii::$app->getUser()->getReturnUrl($defaultUrl));
- }
-
- public function refresh($anchor = '')
- {
- return $this->response->redirect($this->request->getUrl() . $anchor);
- }
- }
|