PageRenderTime 52ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/yiisoft/yii2/web/Controller.php

https://gitlab.com/makkooz/nikestreetbeat
PHP | 221 lines | 86 code | 16 blank | 119 comment | 17 complexity | cbb31af5385aa7ba99006fd2d425e275 MD5 | raw file
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\web;
  8. use Yii;
  9. use yii\base\InlineAction;
  10. use yii\helpers\Url;
  11. /**
  12. * Controller is the base class of web controllers.
  13. *
  14. * @author Qiang Xue <qiang.xue@gmail.com>
  15. * @since 2.0
  16. */
  17. class Controller extends \yii\base\Controller
  18. {
  19. /**
  20. * @var boolean whether to enable CSRF validation for the actions in this controller.
  21. * CSRF validation is enabled only when both this property and [[Request::enableCsrfValidation]] are true.
  22. */
  23. public $enableCsrfValidation = true;
  24. /**
  25. * @var array the parameters bound to the current action.
  26. */
  27. public $actionParams = [];
  28. /**
  29. * Renders a view in response to an AJAX request.
  30. *
  31. * This method is similar to [[renderPartial()]] except that it will inject into
  32. * the rendering result with JS/CSS scripts and files which are registered with the view.
  33. * For this reason, you should use this method instead of [[renderPartial()]] to render
  34. * a view to respond to an AJAX request.
  35. *
  36. * @param string $view the view name. Please refer to [[render()]] on how to specify a view name.
  37. * @param array $params the parameters (name-value pairs) that should be made available in the view.
  38. * @return string the rendering result.
  39. */
  40. public function renderAjax($view, $params = [])
  41. {
  42. return $this->getView()->renderAjax($view, $params, $this);
  43. }
  44. /**
  45. * Binds the parameters to the action.
  46. * This method is invoked by [[\yii\base\Action]] when it begins to run with the given parameters.
  47. * This method will check the parameter names that the action requires and return
  48. * the provided parameters according to the requirement. If there is any missing parameter,
  49. * an exception will be thrown.
  50. * @param \yii\base\Action $action the action to be bound with parameters
  51. * @param array $params the parameters to be bound to the action
  52. * @return array the valid parameters that the action can run with.
  53. * @throws BadRequestHttpException if there are missing or invalid parameters.
  54. */
  55. public function bindActionParams($action, $params)
  56. {
  57. if ($action instanceof InlineAction) {
  58. $method = new \ReflectionMethod($this, $action->actionMethod);
  59. } else {
  60. $method = new \ReflectionMethod($action, 'run');
  61. }
  62. $args = [];
  63. $missing = [];
  64. $actionParams = [];
  65. foreach ($method->getParameters() as $param) {
  66. $name = $param->getName();
  67. if (($class = $param->getClass()) !== null) {
  68. $className = $class->getName();
  69. }
  70. // We only enter the class injection code path if:
  71. // - A class is hinted in the method signature
  72. // - And the param name of hinted class does not exist in existing $params, or the value in existing $params is not an instance of the hinted class
  73. // The latter two checks allow us to manually inject classes via $params while ignoring wrongly injected values (no instances of hinted class).
  74. if ($class !== null && (!array_key_exists($name, $params) || !$params[$name] instanceof $className)) {
  75. if (Yii::$app->has($name) && ($obj = Yii::$app->get($name)) instanceof $className) {
  76. $args[] = $actionParams[$name] = $obj;
  77. } else {
  78. $args[] = $actionParams[$name] = Yii::$container->get($className);
  79. }
  80. } elseif (array_key_exists($name, $params)) {
  81. if ($param->isArray()) {
  82. $args[] = $actionParams[$name] = (array) $params[$name];
  83. } elseif (!is_array($params[$name])) {
  84. $args[] = $actionParams[$name] = $params[$name];
  85. } else {
  86. throw new BadRequestHttpException(Yii::t('yii', 'Invalid data received for parameter "{param}".', [
  87. 'param' => $name,
  88. ]));
  89. }
  90. unset($params[$name]);
  91. } elseif ($param->isDefaultValueAvailable()) {
  92. $args[] = $actionParams[$name] = $param->getDefaultValue();
  93. } else {
  94. $missing[] = $name;
  95. }
  96. }
  97. if (!empty($missing)) {
  98. throw new BadRequestHttpException(Yii::t('yii', 'Missing required parameters: {params}', [
  99. 'params' => implode(', ', $missing),
  100. ]));
  101. }
  102. $this->actionParams = $actionParams;
  103. return $args;
  104. }
  105. /**
  106. * @inheritdoc
  107. */
  108. public function beforeAction($action)
  109. {
  110. if (parent::beforeAction($action)) {
  111. if ($this->enableCsrfValidation && Yii::$app->getErrorHandler()->exception === null && !Yii::$app->getRequest()->validateCsrfToken()) {
  112. throw new BadRequestHttpException(Yii::t('yii', 'Unable to verify your data submission.'));
  113. }
  114. return true;
  115. }
  116. return false;
  117. }
  118. /**
  119. * Redirects the browser to the specified URL.
  120. * This method is a shortcut to [[Response::redirect()]].
  121. *
  122. * You can use it in an action by returning the [[Response]] directly:
  123. *
  124. * ```php
  125. * // stop executing this action and redirect to login page
  126. * return $this->redirect(['login']);
  127. * ```
  128. *
  129. * @param string|array $url the URL to be redirected to. This can be in one of the following formats:
  130. *
  131. * - a string representing a URL (e.g. "http://example.com")
  132. * - a string representing a URL alias (e.g. "@example.com")
  133. * - an array in the format of `[$route, ...name-value pairs...]` (e.g. `['site/index', 'ref' => 1]`)
  134. * [[Url::to()]] will be used to convert the array into a URL.
  135. *
  136. * Any relative URL will be converted into an absolute one by prepending it with the host info
  137. * of the current request.
  138. *
  139. * @param integer $statusCode the HTTP status code. Defaults to 302.
  140. * See <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html>
  141. * for details about HTTP status code
  142. * @return Response the current response object
  143. */
  144. public function redirect($url, $statusCode = 302)
  145. {
  146. return Yii::$app->getResponse()->redirect(Url::to($url), $statusCode);
  147. }
  148. /**
  149. * Redirects the browser to the home page.
  150. *
  151. * You can use this method in an action by returning the [[Response]] directly:
  152. *
  153. * ```php
  154. * // stop executing this action and redirect to home page
  155. * return $this->goHome();
  156. * ```
  157. *
  158. * @return Response the current response object
  159. */
  160. public function goHome()
  161. {
  162. return Yii::$app->getResponse()->redirect(Yii::$app->getHomeUrl());
  163. }
  164. /**
  165. * Redirects the browser to the last visited page.
  166. *
  167. * You can use this method in an action by returning the [[Response]] directly:
  168. *
  169. * ```php
  170. * // stop executing this action and redirect to last visited page
  171. * return $this->goBack();
  172. * ```
  173. *
  174. * For this function to work you have to [[User::setReturnUrl()|set the return URL]] in appropriate places before.
  175. *
  176. * @param string|array $defaultUrl the default return URL in case it was not set previously.
  177. * If this is null and the return URL was not set previously, [[Application::homeUrl]] will be redirected to.
  178. * Please refer to [[User::setReturnUrl()]] on accepted format of the URL.
  179. * @return Response the current response object
  180. * @see User::getReturnUrl()
  181. */
  182. public function goBack($defaultUrl = null)
  183. {
  184. return Yii::$app->getResponse()->redirect(Yii::$app->getUser()->getReturnUrl($defaultUrl));
  185. }
  186. /**
  187. * Refreshes the current page.
  188. * This method is a shortcut to [[Response::refresh()]].
  189. *
  190. * You can use it in an action by returning the [[Response]] directly:
  191. *
  192. * ```php
  193. * // stop executing this action and refresh the current page
  194. * return $this->refresh();
  195. * ```
  196. *
  197. * @param string $anchor the anchor that should be appended to the redirection URL.
  198. * Defaults to empty. Make sure the anchor starts with '#' if you want to specify it.
  199. * @return Response the response object itself
  200. */
  201. public function refresh($anchor = '')
  202. {
  203. return Yii::$app->getResponse()->redirect(Yii::$app->getRequest()->getUrl() . $anchor);
  204. }
  205. }