|
- <?php
-
-
- namespace yii\web;
-
- use Yii;
- use yii\base\Action;
- use yii\base\InvalidParamException;
- use yii\base\ViewNotFoundException;
-
-
- class ViewAction extends Action
- {
-
-
- public $viewParam = 'view';
-
-
- public $defaultView = 'index';
-
-
- public $viewPrefix = 'pages';
-
-
- public $layout;
-
-
-
-
- public function run()
- {
- $viewName = $this->resolveViewName();
- $this->controller->actionParams[$this->viewParam] = Yii::$app->request->get($this->viewParam);
-
- $controllerLayout = null;
- if ($this->layout !== null) {
- $controllerLayout = $this->controller->layout;
- $this->controller->layout = $this->layout;
- }
-
- try {
- $output = $this->render($viewName);
-
- if ($controllerLayout) {
- $this->controller->layout = $controllerLayout;
- }
-
- } catch (ViewNotFoundException $e) {
-
- if ($controllerLayout) {
- $this->controller->layout = $controllerLayout;
- }
-
- if (YII_DEBUG) {
- throw new NotFoundHttpException($e->getMessage());
- } else {
- throw new NotFoundHttpException(
- Yii::t('yii', 'The requested view "{name}" was not found.', ['name' => $viewName])
- );
- }
- }
-
- return $output;
- }
-
-
-
- protected function render($viewName)
- {
- return $this->controller->render($viewName);
- }
-
-
-
- protected function resolveViewName()
- {
- $viewName = Yii::$app->request->get($this->viewParam, $this->defaultView);
-
- if (!is_string($viewName) || !preg_match('~^\w(?:(?!\/\.{0,2}\/)[\w\/\-\.])*$~', $viewName)) {
- if (YII_DEBUG) {
- 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.");
- } else {
- throw new NotFoundHttpException(Yii::t('yii', 'The requested view "{name}" was not found.', ['name' => $viewName]));
- }
- }
-
- return empty($this->viewPrefix) ? $viewName : $this->viewPrefix . '/' . $viewName;
- }
- }
|