Commit 9936acf7 by Alexander Makarov

Fixes #2932: Added `yii\web\ViewAction` that allow you to render views based on GET parameter

parent 271dccdc
...@@ -276,6 +276,7 @@ _ Chg #2912: Relative view files will be looked for under the directory containi ...@@ -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 #1956: Implemented test fixture framework (qiangxue)
- New #2149: Added `yii\base\DynamicModel` to support ad-hoc data validation (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 #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: Yii framework now comes with core messages in multiple languages
- New: Added `yii\codeception\DbTestCase` (qiangxue) - New: Added `yii\codeception\DbTestCase` (qiangxue)
- New: Added `yii\web\PrefixUrlRule` (qiangxue) - New: Added `yii\web\PrefixUrlRule` (qiangxue)
......
<?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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment