View-Komponente verwenden, wenn ich Ansicht Komponente wie das Dokument zu verwenden, sagtwie funktioniert das Yii2 das @ app/views/layout/main.php mit @ app/themes/basic/Layout/main.php ersetzen, wenn ich
return [
'components' => [
'view' => [
'theme' => [
'basePath' => '@app/themes/basic',
'baseUrl' => '@web/themes/basic',
'pathMap' => [
'@app/views' => '@app/themes/basic',
],
],
],
],
];
bedeutet dies, dass die main.php im Ordner @ app/themes/basic/layouts/main.php die main.php im Ordner @ app/views/layouts/main.php ersetzt?
und wie yii2 dies erreichen? Ich konnte es nicht herausfinden, obwohl ich den Controller nachverfolgte, um etwas zu finden, das mir helfen könnte, die Logik zu verstehen.
Ich verfolgt die Codes wie folgt, aber ich habe nicht herausgefunden, wie das Layout main.php bei der Verwendung von Themen ersetzt wurde.
1.SiteController, Standardaktion Index, machen dann die Ansicht
public function actionIndex()
{
return $this->render('index');
}
2.call der Controller-Klasse Funktion
public function render($view, $params = [])
{
$content = $this->getView()->render($view, $params, $this);
return $this->renderContent($content);
}
3.Get der yii \ web \ Ansicht
machenpublic function getView()
{
if ($this->_view === null) {
$this->_view = Yii::$app->getView();
}
return $this->_view;
}
4.Verwenden Sie die View-Klassenfunktion render
public function render($view, $params = [], $context = null)
{
$viewFile = $this->findViewFile($view, $context);
return $this->renderFile($viewFile, $params, $context);
}
5.find View-Datei erhalten Sie die Ansicht Dateipfad
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 dieser Ansicht Datei
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 das Thema nicht null ist, wird das System nennen $ this-> themen-> AnwendenAuf ($ View) das Thema Ansicht Datei zu erhalten, und renderPhpFile
$output = $this->renderPhpFile($viewFile, $params);
8.get der conten t dieser Datei
dank für Ihre Antwort, ich habe diese Dokumente gelesen, aber sie erklären nicht, wie die main.php im Layout durch die main.php im Theme-Ordner – Arron
ersetzt wurde, weil alle Referenzen auf yourapp/views durch alle Referenzen auf Theme Views ersetzt wurden (Website und Layout-Rendering enthalten) .. – scaisEdge
ich danke Ihnen noch einmal, ich habe den Quellcode gelesen und versucht, ihre Logik zu verstehen. nur ein wenig verwirrt darüber, weil ich die Codes zum Ersetzen von main.php in @ app/view mit main.php in Theme-Ordner-Layouts nicht finde – Arron