[英]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.php文件夾中的main.php?
以及yii2如何實現這一目標? 盡管我跟蹤控制器找到了可以幫助我理解邏輯的東西,但我不知道。
我跟蹤了以下代碼,但是當使用主題時,我沒有發現如何替換布局main.php。
1.SiteController,默認動作索引,然后渲染視圖
public function actionIndex()
{
return $this->render('index');
}
2.調用Controller類的函數render
public function render($view, $params = [])
{
$content = $this->getView()->render($view, $params, $this);
return $this->renderContent($content);
}
3.獲取yii \\ web \\ View
public function getView()
{
if ($this->_view === null) {
$this->_view = Yii::$app->getView();
}
return $this->_view;
}
4,使用View類函數渲染
public function render($view, $params = [], $context = null)
{
$viewFile = $this->findViewFile($view, $context);
return $this->renderFile($viewFile, $params, $context);
}
5.查找視圖文件,獲取視圖文件路徑
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.渲染該視圖文件
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,如果主題不為空,系統將調用$ this-> theme-> applyTo($ viewFile)獲取主題視圖文件和renderPhpFile
$output = $this->renderPhpFile($viewFile, $params);
8.獲取此文件的內容
發生這種情況的原因是,當Yii2在config / components中執行渲染檢查時,視圖容器目錄的真實映射。
這可以與您的情況下的所有視圖相關
'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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.