如果我使用視圖組件,如文件說怎樣的Yii2更換@應用程序/視圖/佈局/用@應用程序/主題/基本/佈局/ main.php main.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.php中的main.php?
以及yii2如何實現這一目標?我無法弄清楚,儘管我追蹤了控制器,發現了一些可以幫助我理解邏輯的東西。
我追蹤了下面的代碼,但是我沒有發現在使用主題時佈局main.php是如何被替換的。
1.SiteController,默認操作指數,然後渲染視圖
public function actionIndex()
{
return $this->render('index');
}
2.call Controller類功能使
public function render($view, $params = [])
{
$content = $this->getView()->render($view, $params, $this);
return $this->renderContent($content);
}
3.get的警予\網絡\查看
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.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.如果主題不爲空,則系統會調用$ this-> theme-> applyTo($ viewFile)來獲取主題視圖文件和renderPhpFile
$output = $this->renderPhpFile($viewFile, $params);
8.get the conten t的文件
謝謝您的回答,我已經閱讀了這些文檔,但是他們沒有解釋如何在佈局中的main.php被main.php替換爲主題文件夾 – Arron
,因爲所有對yourapp/views的引用都被主題視圖的所有引用所取代(包括網站和佈局渲染).. – scaisEdge
再次感謝你,我一直在閱讀源代碼,試圖理解他們的邏輯。只是有點困惑,因爲我沒有找到有關在主題文件夾佈局中使用main.php替換@ app/view中的main.php的代碼 – Arron