ViewAction.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\ViewNotFoundException;
  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. $this->controller->actionParams[$this->viewParam] = Yii::$app->request->get($this->viewParam);
  65. $controllerLayout = null;
  66. if ($this->layout !== null) {
  67. $controllerLayout = $this->controller->layout;
  68. $this->controller->layout = $this->layout;
  69. }
  70. try {
  71. $output = $this->render($viewName);
  72. if ($controllerLayout) {
  73. $this->controller->layout = $controllerLayout;
  74. }
  75. } catch (ViewNotFoundException $e) {
  76. if ($controllerLayout) {
  77. $this->controller->layout = $controllerLayout;
  78. }
  79. if (YII_DEBUG) {
  80. throw new NotFoundHttpException($e->getMessage());
  81. }
  82. throw new NotFoundHttpException(
  83. Yii::t('yii', 'The requested view "{name}" was not found.', ['name' => $viewName])
  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(?:(?!\/\.{0,2}\/)[\w\/\-\.])*$~', $viewName)) {
  108. if (YII_DEBUG) {
  109. throw new NotFoundHttpException("The requested view \"$viewName\" must start with a word character, must not contain /../ or /./, can contain only word characters, forward slashes, dots and dashes.");
  110. }
  111. throw new NotFoundHttpException(Yii::t('yii', 'The requested view "{name}" was not found.', ['name' => $viewName]));
  112. }
  113. return empty($this->viewPrefix) ? $viewName : $this->viewPrefix . '/' . $viewName;
  114. }
  115. }