From 9936acf764d532cf52d572d2fefefe7a43d2e041 Mon Sep 17 00:00:00 2001 From: Alexander Makarov <sam@rmcreative.ru> Date: Tue, 1 Apr 2014 00:41:21 +0400 Subject: [PATCH] Fixes #2932: Added `yii\web\ViewAction` that allow you to render views based on GET parameter --- framework/CHANGELOG.md | 1 + framework/web/ViewAction.php | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 framework/web/ViewAction.php diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index e5f74d4..bf683d0 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -276,6 +276,7 @@ _ Chg #2912: Relative view files will be looked for under the directory containi - New #1956: Implemented test fixture framework (qiangxue) - New #2149: Added `yii\base\DynamicModel` to support ad-hoc data validation (qiangxue) - New #2360: Added `AttributeBehavior` and `BlameableBehavior`, and renamed `AutoTimestamp` to `TimestampBehavior` (lucianobaraglia, qiangxue) +- New #2932: Added `yii\web\ViewAction` that allow you to render views based on GET parameter (samdark) - New: Yii framework now comes with core messages in multiple languages - New: Added `yii\codeception\DbTestCase` (qiangxue) - New: Added `yii\web\PrefixUrlRule` (qiangxue) diff --git a/framework/web/ViewAction.php b/framework/web/ViewAction.php new file mode 100644 index 0000000..2f4bc8a --- /dev/null +++ b/framework/web/ViewAction.php @@ -0,0 +1,138 @@ +<?php +/** + * @link http://www.yiiframework.com/ + * @copyright Copyright (c) 2008 Yii Software LLC + * @license http://www.yiiframework.com/license/ + */ + +namespace yii\web; + +use yii\base\Action; +use yii\base\InvalidParamException; + +/** + * ViewAction represents an action that displays a view according to a user-specified parameter. + * + * By default, the view being displayed is specified via the `view` GET parameter. + * The name of the GET parameter can be customized via [[\yii\base\ViewAction::$viewParam]]. + * If the user doesn't provide the GET parameter, the default view specified by [[\yii\base\ViewAction::$defaultView]] + * will be displayed. + * + * Users specify a view in the format of `path/to/view`, which translates to the view name + * `BasePath/path/to/view` where `BasePath` is given by [[\yii\base\ViewAction::$basePath]]. + * + * Note, the user specified view can only contain word characters, dots and dashes and + * the first letter must be a word letter. + * + * @property string $requestedView The name of the view requested by the user. + * This is in the format of 'path/to/view'. + * + * @author Alexander Makarov <sam@rmcreative.ru> + * @author Qiang Xue <qiang.xue@gmail.com> + * @since 2.0 + */ +class ViewAction extends Action +{ + /** + * @var string the name of the GET parameter that contains the requested view name. Defaults to 'view'. + */ + public $viewParam = 'view'; + + /** + * @var string the name of the default view when [[\yii\base\ViewAction::$viewParam]] GET parameter is not provided + * by user. Defaults to 'index'. This should be in the format of 'path/to/view', similar to that given in + * the GET parameter. + * @see \yii\base\ViewAction::$basePath + */ + public $defaultView = 'index'; + + /** + * @var string the base path for the views. Defaults to 'pages'. + * The base path will be prefixed to any user-specified page view. + * For example, if a user requests for `tutorial/chap1`, the corresponding view name will + * be `pages/tutorial/chap1`, assuming the base path is `pages`. + * The actual view file is determined by [[\yii\base\View::getViewFile()]]. + * @see \yii\base\View::getViewFile() + */ + public $basePath = 'pages'; + + /** + * @var mixed the name of the layout to be applied to the views. + * This will be assigned to [[\yii\base\Controller::$layout]] before the view is rendered. + * Defaults to null, meaning the controller's layout will be used. + * If false, no layout will be applied. + */ + public $layout; + + /** + * @var string Used to store controller layout during executin and then restore it + */ + private $_controllerLayout; + + /** + * Runs the action. + * This method displays the view requested by the user. + * @throws NotFoundHttpException if the view file cannot be found + */ + public function run() + { + $viewPath = $this->getViewPath(); + + if($this->layout !== null) { + $this->_controllerLayout = $this->controller->layout; + $this->controller->layout = $this->layout; + } + + try { + return $this->controller->render($viewPath); + } catch (InvalidParamException $e) { + if (YII_DEBUG) { + throw new NotFoundHttpException($e->getMessage()); + } else { + throw new NotFoundHttpException( + \Yii::t('yii', 'The requested view "{name}" was not found.', ['name' => $viewPath]) + ); + } + } + } + + /** + * @inheritdoc + */ + protected function afterRun() + { + if ($this->layout !== null) { + $this->controller->layout = $this->_controllerLayout; + } + parent::afterRun(); + } + + /** + * Obtain view path from GET + * + * @return string view path + * @throws NotFoundHttpException if view path doesn't match allowed format + */ + protected function getViewPath() + { + $viewPath = \Yii::$app->request->get($this->viewParam); + if (empty($viewPath) || !is_string($viewPath)) { + $viewPath = $this->defaultView; + } + + if (!preg_match('/^\w[\w\/\-]*$/', $viewPath)) { + if (YII_DEBUG) { + throw new NotFoundHttpException("The requested view \"$viewPath\" should start with a word char and contain word chars, forward slashes and dashes only."); + } else { + throw new NotFoundHttpException(\Yii::t('yii', 'The requested view "{name}" was not found.', ['name' => $viewPath])); + } + } + + if (!empty($this->basePath)) { + $viewPath = $this->basePath . '/' . $viewPath; + return $viewPath; + } + return $viewPath; + } +} + \ No newline at end of file -- libgit2 0.27.1