From d08ef83e2ad2d9b9813ec558d1adabfe7a90fc28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Hern=C3=A1ndez?= Date: Sat, 21 Nov 2015 19:28:06 +0200 Subject: [PATCH] Controller->viewPath can be set, fixes #10086 Added setViewPath() that allows setting the base path for the controller's views. Changed getViewPath() to return the value that was explicitly set, before defaulting to the usual module-based path. --- framework/CHANGELOG.md | 1 + framework/base/Controller.php | 23 ++++++++++++++++++++--- framework/base/Module.php | 7 +++---- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index ea2e709327..717fc3f9f8 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -61,6 +61,7 @@ Yii Framework 2 Change Log - Enh #10056: Allowed any callable to be passed to `ActionColumn::$urlCreator` (freezy-sk) - Enh #10061: `yii\helpers\BaseInflector::transliterate()` is now public. Introduced different levels of transliteration strictness (silverfire) - Enh #10078: Added `csrf` option to `Html::beginForm()` to allow disabling the hidden csrf field generation (machour) +- Enh #10086: `yii\base\Controller::viewPath` is now configurable (Sibilino) - Enh #10098: Changed `yii.confirm` context to the event's target DOM element which is triggered by clickable or changeable elements (lichunqiang) - Enh #10118: Allow easy extension of slug generation in `yii\behaviors\SluggableBehavior` (cebe, hesna) - Enh #10149: Made `yii\db\Connection` serializable (Sam Mousa) diff --git a/framework/base/Controller.php b/framework/base/Controller.php index aef46e9844..19eb478cee 100644 --- a/framework/base/Controller.php +++ b/framework/base/Controller.php @@ -19,8 +19,7 @@ use Yii; * @property string $uniqueId The controller ID that is prefixed with the module ID (if any). This property is * read-only. * @property View|\yii\web\View $view The view object that can be used to render views or view files. - * @property string $viewPath The directory containing the view files for this controller. This property is - * read-only. + * @property string $viewPath The directory containing the view files for this controller. * * @author Qiang Xue * @since 2.0 @@ -67,6 +66,10 @@ class Controller extends Component implements ViewContextInterface * @var View the view object that can be used to render views or view files. */ private $_view; + /** + * @var string the root directory that contains view files for this controller. + */ + private $_viewPath; /** @@ -449,7 +452,21 @@ class Controller extends Component implements ViewContextInterface */ public function getViewPath() { - return $this->module->getViewPath() . DIRECTORY_SEPARATOR . $this->id; + if ($this->_viewPath === null) { + $this->_viewPath = $this->module->getViewPath() . DIRECTORY_SEPARATOR . $this->id; + } + return $this->_viewPath; + } + + /** + * Sets the directory that contains the view files. + * @param string $path the root directory of view files. + * @throws InvalidParamException if the directory is invalid + * @since 2.0.7 + */ + public function setViewPath($path) + { + $this->_viewPath = Yii::getAlias($path); } /** diff --git a/framework/base/Module.php b/framework/base/Module.php index 72eece53f4..5ffcd5da29 100644 --- a/framework/base/Module.php +++ b/framework/base/Module.php @@ -243,11 +243,10 @@ class Module extends ServiceLocator */ public function getViewPath() { - if ($this->_viewPath !== null) { - return $this->_viewPath; - } else { - return $this->_viewPath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'views'; + if ($this->_viewPath === null) { + $this->_viewPath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'views'; } + return $this->_viewPath; } /**