mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-11 19:20:01 +08:00
minor refactoring of view resolving code.
This commit is contained in:
@@ -225,8 +225,8 @@ class View extends Component
|
|||||||
* on how to specify this parameter.
|
* on how to specify this parameter.
|
||||||
* @param object $context the context that the view should be used to search the view file. If null,
|
* @param object $context the context that the view should be used to search the view file. If null,
|
||||||
* existing [[context]] will be used.
|
* existing [[context]] will be used.
|
||||||
* @throws InvalidCallException if [[context]] is required and invalid.
|
|
||||||
* @return string the view file path. Note that the file may not exist.
|
* @return string the view file path. Note that the file may not exist.
|
||||||
|
* @throws InvalidCallException if [[context]] is required and invalid.
|
||||||
*/
|
*/
|
||||||
protected function findViewFile($view, $context = null)
|
protected function findViewFile($view, $context = null)
|
||||||
{
|
{
|
||||||
@@ -236,9 +236,13 @@ class View extends Component
|
|||||||
} elseif (strncmp($view, '//', 2) === 0) {
|
} elseif (strncmp($view, '//', 2) === 0) {
|
||||||
// e.g. "//layouts/main"
|
// e.g. "//layouts/main"
|
||||||
$file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
|
$file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
|
||||||
} elseif (strncmp($view, '/', 1) === 0 && Yii::$app->controller !== null) {
|
} elseif (strncmp($view, '/', 1) === 0) {
|
||||||
// e.g. "/site/index"
|
// e.g. "/site/index"
|
||||||
$file = Yii::$app->controller->module->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
|
if (Yii::$app->controller !== null) {
|
||||||
|
$file = Yii::$app->controller->module->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
|
||||||
|
} else {
|
||||||
|
throw new InvalidCallException("Unable to locate view file for view '$view': no active controller.");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// context required
|
// context required
|
||||||
if ($context === null) {
|
if ($context === null) {
|
||||||
@@ -247,7 +251,7 @@ class View extends Component
|
|||||||
if ($context instanceof ViewContextInterface) {
|
if ($context instanceof ViewContextInterface) {
|
||||||
$file = $context->findViewFile($view);
|
$file = $context->findViewFile($view);
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidCallException('Current context is not supported.');
|
throw new InvalidCallException("Unable to locate view file for view '$view': no active view context.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,13 +8,9 @@
|
|||||||
namespace yii\base;
|
namespace yii\base;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ViewContextInterface represents possible context for the view rendering.
|
* ViewContextInterface is the interface that should implemented by classes who want to support relative view names.
|
||||||
* It determines the way the non-global view files are searched.
|
|
||||||
* This interface introduces method [[findViewFile]], which will be used
|
|
||||||
* at [[View::render()]] to determine the file by view name, which does
|
|
||||||
* not match default format.
|
|
||||||
*
|
*
|
||||||
* @see View
|
* The method [[findViewFile()]] should be implemented to convert a relative view name into a file path.
|
||||||
*
|
*
|
||||||
* @author Paul Klimov <klimov.paul@gmail.com>
|
* @author Paul Klimov <klimov.paul@gmail.com>
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
@@ -22,9 +18,9 @@ namespace yii\base;
|
|||||||
interface ViewContextInterface
|
interface ViewContextInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Finds the view file based on the given view name.
|
* Finds the view file corresponding to the specified relative view name.
|
||||||
* @param string $view the view name.
|
* @param string $view a relative view name. The name does NOT start with a slash.
|
||||||
* @return string the view file path. Note that the file may not exist.
|
* @return string the view file path. Note that the file may not exist.
|
||||||
*/
|
*/
|
||||||
public function findViewFile($view);
|
public function findViewFile($view);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
namespace yii\base;
|
namespace yii\base;
|
||||||
|
|
||||||
use Yii;
|
use Yii;
|
||||||
|
use ReflectionClass;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Widget is the base class for widgets.
|
* Widget is the base class for widgets.
|
||||||
@@ -189,8 +190,7 @@ class Widget extends Component implements ViewContextInterface
|
|||||||
*/
|
*/
|
||||||
public function getViewPath()
|
public function getViewPath()
|
||||||
{
|
{
|
||||||
$className = get_class($this);
|
$class = new ReflectionClass($this);
|
||||||
$class = new \ReflectionClass($className);
|
|
||||||
return dirname($class->getFileName()) . DIRECTORY_SEPARATOR . 'views';
|
return dirname($class->getFileName()) . DIRECTORY_SEPARATOR . 'views';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,6 +202,6 @@ class Widget extends Component implements ViewContextInterface
|
|||||||
*/
|
*/
|
||||||
public function findViewFile($view)
|
public function findViewFile($view)
|
||||||
{
|
{
|
||||||
return $this->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
|
return $this->getViewPath() . DIRECTORY_SEPARATOR . $view;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user