当我使用视图组件时,Yii2 如何将 @app/views/layouts/main.php 替换为 @app/themes/basic/layouts/main.php
how does the Yii2 replace the @app/views/layouts/main.php with @app/themes/basic/layouts/main.php when I use view component
如果我像文档中说的那样使用视图组件
return [
'components' => [
'view' => [
'theme' => [
'basePath' => '@app/themes/basic',
'baseUrl' => '@web/themes/basic',
'pathMap' => [
'@app/views' => '@app/themes/basic',
],
],
],
],
];
是否表示文件夹@app/themes/basic/layouts/main.php中的main.php将替换文件夹@app/views/layouts/main.[=中的main.php 60=] ?
yii2 是如何做到这一点的?虽然我跟踪控制器找到了一些可以帮助我理解逻辑的东西,但我无法弄清楚。
我跟踪代码如下,但我没有发现使用主题时布局main.php是如何被替换的。
1.SiteController,默认动作索引,然后渲染视图
public function actionIndex()
{
return $this->render('index');
}
2.call控制器class函数渲染
public function render($view, $params = [])
{
$content = $this->getView()->render($view, $params, $this);
return $this->renderContent($content);
}
3.get yii\web\View
public function getView()
{
if ($this->_view === null) {
$this->_view = Yii::$app->getView();
}
return $this->_view;
}
4.use View class 函数渲染
public function render($view, $params = [], $context = null)
{
$viewFile = $this->findViewFile($view, $context);
return $this->renderFile($viewFile, $params, $context);
}
5.find查看文件,获取查看文件路径
protected function findViewFile($view, $context = null)
{
if (strncmp($view, '@', 1) === 0) {
// e.g. "@app/views/main"
$file = Yii::getAlias($view);
} elseif (strncmp($view, '//', 2) === 0) {
// e.g. "//layouts/main"
$file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
} elseif (strncmp($view, '/', 1) === 0) {
// e.g. "/site/index"
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.");
}
} elseif ($context instanceof ViewContextInterface) {
$file = $context->getViewPath() . DIRECTORY_SEPARATOR . $view;
} elseif (($currentViewFile = $this->getViewFile()) !== false) {
$file = dirname($currentViewFile) . DIRECTORY_SEPARATOR . $view;
} else {
throw new InvalidCallException("Unable to resolve view file for view '$view': no active view context.");
}
if (pathinfo($file, PATHINFO_EXTENSION) !== '') {
return $file;
}
$path = $file . '.' . $this->defaultExtension;
if ($this->defaultExtension !== 'php' && !is_file($path)) {
$path = $file . '.php';
}
return $path;
}
6.render这个查看文件
public function renderFile($viewFile, $params = [], $context = null)
{
$viewFile = Yii::getAlias($viewFile);
if ($this->theme !== null) {
$viewFile = $this->theme->applyTo($viewFile);
}
if (is_file($viewFile)) {
$viewFile = FileHelper::localize($viewFile);
} else {
throw new ViewNotFoundException("The view file does not exist: $viewFile");
}
$oldContext = $this->context;
if ($context !== null) {
$this->context = $context;
}
$output = '';
$this->_viewFiles[] = $viewFile;
if ($this->beforeRender($viewFile, $params)) {
Yii::trace("Rendering view file: $viewFile", __METHOD__);
$ext = pathinfo($viewFile, PATHINFO_EXTENSION);
if (isset($this->renderers[$ext])) {
if (is_array($this->renderers[$ext]) || is_string($this->renderers[$ext])) {
$this->renderers[$ext] = Yii::createObject($this->renderers[$ext]);
}
/* @var $renderer ViewRenderer */
$renderer = $this->renderers[$ext];
$output = $renderer->render($this, $viewFile, $params);
} else {
$output = $this->renderPhpFile($viewFile, $params);
}
$this->afterRender($viewFile, $params, $output);
}
array_pop($this->_viewFiles);
$this->context = $oldContext;
return $output;
}
7.if主题不为空,系统会调用$this->theme->applyTo($viewFile)获取主题视图文件,renderPhpFile
$output = $this->renderPhpFile($viewFile, $params);
8.get这个文件的内容
发生这种情况是因为当 Yii2 在配置/组件中执行渲染检查时,视图容器目录的真实映射。
这可能与您的情况下的所有视图相关
'view' => [
'theme' => [
'basePath' => '@app/themes/basic',
'baseUrl' => '@web/themes/basic',
'pathMap' => [
'@app/views' => '@app/themes/basic',
],
],
],
或者可以与某些供应商或模数相关,如
'view' => [
'theme' => [
'pathMap' => [
'@dektrium/user/views' => '@backend/views/my-view-user' // mapping per overriding s dektrium views with personal views
],
],
],
您可以查看参考文档以获取意见http://www.yiiframework.com/doc-2.0/yii-base-view.html
和https://github.com/yiisoft/yii2/blob/master/framework/base/View.php
如果我像文档中说的那样使用视图组件
return [
'components' => [
'view' => [
'theme' => [
'basePath' => '@app/themes/basic',
'baseUrl' => '@web/themes/basic',
'pathMap' => [
'@app/views' => '@app/themes/basic',
],
],
],
],
];
是否表示文件夹@app/themes/basic/layouts/main.php中的main.php将替换文件夹@app/views/layouts/main.[=中的main.php 60=] ?
yii2 是如何做到这一点的?虽然我跟踪控制器找到了一些可以帮助我理解逻辑的东西,但我无法弄清楚。
我跟踪代码如下,但我没有发现使用主题时布局main.php是如何被替换的。
1.SiteController,默认动作索引,然后渲染视图
public function actionIndex()
{
return $this->render('index');
}
2.call控制器class函数渲染
public function render($view, $params = [])
{
$content = $this->getView()->render($view, $params, $this);
return $this->renderContent($content);
}
3.get yii\web\View
public function getView()
{
if ($this->_view === null) {
$this->_view = Yii::$app->getView();
}
return $this->_view;
}
4.use View class 函数渲染
public function render($view, $params = [], $context = null)
{
$viewFile = $this->findViewFile($view, $context);
return $this->renderFile($viewFile, $params, $context);
}
5.find查看文件,获取查看文件路径
protected function findViewFile($view, $context = null)
{
if (strncmp($view, '@', 1) === 0) {
// e.g. "@app/views/main"
$file = Yii::getAlias($view);
} elseif (strncmp($view, '//', 2) === 0) {
// e.g. "//layouts/main"
$file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
} elseif (strncmp($view, '/', 1) === 0) {
// e.g. "/site/index"
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.");
}
} elseif ($context instanceof ViewContextInterface) {
$file = $context->getViewPath() . DIRECTORY_SEPARATOR . $view;
} elseif (($currentViewFile = $this->getViewFile()) !== false) {
$file = dirname($currentViewFile) . DIRECTORY_SEPARATOR . $view;
} else {
throw new InvalidCallException("Unable to resolve view file for view '$view': no active view context.");
}
if (pathinfo($file, PATHINFO_EXTENSION) !== '') {
return $file;
}
$path = $file . '.' . $this->defaultExtension;
if ($this->defaultExtension !== 'php' && !is_file($path)) {
$path = $file . '.php';
}
return $path;
}
6.render这个查看文件
public function renderFile($viewFile, $params = [], $context = null)
{
$viewFile = Yii::getAlias($viewFile);
if ($this->theme !== null) {
$viewFile = $this->theme->applyTo($viewFile);
}
if (is_file($viewFile)) {
$viewFile = FileHelper::localize($viewFile);
} else {
throw new ViewNotFoundException("The view file does not exist: $viewFile");
}
$oldContext = $this->context;
if ($context !== null) {
$this->context = $context;
}
$output = '';
$this->_viewFiles[] = $viewFile;
if ($this->beforeRender($viewFile, $params)) {
Yii::trace("Rendering view file: $viewFile", __METHOD__);
$ext = pathinfo($viewFile, PATHINFO_EXTENSION);
if (isset($this->renderers[$ext])) {
if (is_array($this->renderers[$ext]) || is_string($this->renderers[$ext])) {
$this->renderers[$ext] = Yii::createObject($this->renderers[$ext]);
}
/* @var $renderer ViewRenderer */
$renderer = $this->renderers[$ext];
$output = $renderer->render($this, $viewFile, $params);
} else {
$output = $this->renderPhpFile($viewFile, $params);
}
$this->afterRender($viewFile, $params, $output);
}
array_pop($this->_viewFiles);
$this->context = $oldContext;
return $output;
}
7.if主题不为空,系统会调用$this->theme->applyTo($viewFile)获取主题视图文件,renderPhpFile
$output = $this->renderPhpFile($viewFile, $params);
8.get这个文件的内容
发生这种情况是因为当 Yii2 在配置/组件中执行渲染检查时,视图容器目录的真实映射。
这可能与您的情况下的所有视图相关
'view' => [
'theme' => [
'basePath' => '@app/themes/basic',
'baseUrl' => '@web/themes/basic',
'pathMap' => [
'@app/views' => '@app/themes/basic',
],
],
],
或者可以与某些供应商或模数相关,如
'view' => [
'theme' => [
'pathMap' => [
'@dektrium/user/views' => '@backend/views/my-view-user' // mapping per overriding s dektrium views with personal views
],
],
],
您可以查看参考文档以获取意见http://www.yiiframework.com/doc-2.0/yii-base-view.html
和https://github.com/yiisoft/yii2/blob/master/framework/base/View.php