選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ViewAction.php 4.7KB

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