Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

133 lines
4.5KB

  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\Action;
  10. use yii\base\InvalidParamException;
  11. /**
  12. * ViewAction represents an action that displays a view according to a user-specified parameter.
  13. *
  14. * By default, the view being displayed is specified via the `view` GET parameter.
  15. * The name of the GET parameter can be customized via [[viewParam]].
  16. *
  17. * Users specify a view in the format of `path/to/view`, which translates to the view name
  18. * `ViewPrefix/path/to/view` where `ViewPrefix` is given by [[viewPrefix]]. The view will then
  19. * be rendered by the [[\yii\base\Controller::render()|render()]] method of the currently active controller.
  20. *
  21. * Note that the user-specified view name must start with a word character and can only contain
  22. * word characters, forward slashes, dots and dashes.
  23. *
  24. * @author Alexander Makarov <sam@rmcreative.ru>
  25. * @author Qiang Xue <qiang.xue@gmail.com>
  26. * @since 2.0
  27. */
  28. class ViewAction extends Action
  29. {
  30. /**
  31. * @var string the name of the GET parameter that contains the requested view name.
  32. */
  33. public $viewParam = 'view';
  34. /**
  35. * @var string the name of the default view when [[\yii\web\ViewAction::$viewParam]] GET parameter is not provided
  36. * by user. Defaults to 'index'. This should be in the format of 'path/to/view', similar to that given in the
  37. * GET parameter.
  38. * @see \yii\web\ViewAction::$viewPrefix
  39. */
  40. public $defaultView = 'index';
  41. /**
  42. * @var string a string to be prefixed to the user-specified view name to form a complete view name.
  43. * For example, if a user requests for `tutorial/chap1`, the corresponding view name will
  44. * be `pages/tutorial/chap1`, assuming the prefix is `pages`.
  45. * The actual view file is determined by [[\yii\base\View::findViewFile()]].
  46. * @see \yii\base\View::findViewFile()
  47. */
  48. public $viewPrefix = 'pages';
  49. /**
  50. * @var mixed the name of the layout to be applied to the requested view.
  51. * This will be assigned to [[\yii\base\Controller::$layout]] before the view is rendered.
  52. * Defaults to null, meaning the controller's layout will be used.
  53. * If false, no layout will be applied.
  54. */
  55. public $layout;
  56. /**
  57. * Runs the action.
  58. * This method displays the view requested by the user.
  59. * @throws NotFoundHttpException if the view file cannot be found
  60. */
  61. public function run()
  62. {
  63. $viewName = $this->resolveViewName();
  64. $controllerLayout = null;
  65. if ($this->layout !== null) {
  66. $controllerLayout = $this->controller->layout;
  67. $this->controller->layout = $this->layout;
  68. }
  69. try {
  70. $output = $this->render($viewName);
  71. if ($controllerLayout) {
  72. $this->controller->layout = $controllerLayout;
  73. }
  74. } catch (InvalidParamException $e) {
  75. if ($controllerLayout) {
  76. $this->controller->layout = $controllerLayout;
  77. }
  78. if (YII_DEBUG) {
  79. throw new NotFoundHttpException($e->getMessage());
  80. } else {
  81. throw new NotFoundHttpException(
  82. Yii::t('yii', 'The requested view "{name}" was not found.', ['name' => $viewName])
  83. );
  84. }
  85. }
  86. return $output;
  87. }
  88. /**
  89. * Renders a view
  90. *
  91. * @param string $viewName view name
  92. * @return string result of the rendering
  93. */
  94. protected function render($viewName)
  95. {
  96. return $this->controller->render($viewName);
  97. }
  98. /**
  99. * Resolves the view name currently being requested.
  100. *
  101. * @return string the resolved view name
  102. * @throws NotFoundHttpException if the specified view name is invalid
  103. */
  104. protected function resolveViewName()
  105. {
  106. $viewName = Yii::$app->request->get($this->viewParam, $this->defaultView);
  107. if (!is_string($viewName) || !preg_match('/^\w[\w\/\-\.]*$/', $viewName)) {
  108. if (YII_DEBUG) {
  109. throw new NotFoundHttpException("The requested view \"$viewName\" must start with a word character and can contain only word characters, forward slashes, dots and dashes.");
  110. } else {
  111. throw new NotFoundHttpException(Yii::t('yii', 'The requested view "{name}" was not found.', ['name' => $viewName]));
  112. }
  113. }
  114. return empty($this->viewPrefix) ? $viewName : $this->viewPrefix . '/' . $viewName;
  115. }
  116. }