Yii - Theming
Il tema ti aiuta a sostituire un insieme di viste con un altro senza la necessità di modificare i file di vista originali. Dovresti impostare il filetheme proprietà del componente dell'applicazione di visualizzazione per utilizzare il tema.
Dovresti anche definire le seguenti proprietà:
yii\base\Theme::$basePath - Definisce la directory di base per CSS, JS, immagini e così via.
yii\base\Theme::$baseUrl - Definisce l'URL di base delle risorse a tema.
yii\base\Theme::$pathMap - Definisce le regole di sostituzione.
Ad esempio, se chiami $this->render('create') in UserController, il @app/views/user/create.phpil file di visualizzazione verrà renderizzato. Tuttavia, se abiliti il tema come nella seguente configurazione dell'applicazione, verrà invece visualizzato il file di visualizzazione @ app / temi / basic / user / create.php.
Step 1 - Modifica il file config/web.php archiviare in questo modo.
<?php
$params = require(__DIR__ . '/params.php'); $config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this
//is required by cookie validation
'cookieValidationKey' => 'ymoaYrebZHa8gURuolioHGlK8fLXCKjO',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'view' => [
'theme' => [
'basePath' => '@app/themes/basic',
'baseUrl' => '@web/themes/basic',
'pathMap' => [
'@app/views' => '@app/themes/basic',
],
],
],
'db' => require(__DIR__ . '/db.php'),
],
'modules' => [
'hello' => [
'class' => 'app\modules\hello\Hello',
],
],
'params' => $params, ]; if (YII_ENV_DEV) { // configuration adjustments for 'dev' environment $config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [ 'class' => 'yii\debug\Module', ]; $config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [ 'class' => 'yii\gii\Module', ]; } return $config;
?>
Abbiamo aggiunto il componente dell'applicazione di visualizzazione.
Step 2 - Ora crea il file web/themes/basic struttura delle directory e themes/basic/site. All'interno della cartella temi / basic / site crea un file chiamatoabout.php con il seguente codice.
<?php
/* @var $this yii\web\View */ use yii\helpers\Html; $this->title = 'About';
$this->params['breadcrumbs'][] = $this->title;
$this->registerMetaTag(['name' => 'keywords', 'content' => 'yii, developing, views, meta, tags']); $this->registerMetaTag(['name' => 'description', 'content' => 'This is the
description of this page!'], 'description');
?>
<div class = "site-about">
<h1><?= Html::encode($this->title) ?></h1>
<p style = "color: red;">
This is the About page. You may modify the following file to customize its content:
</p>
</div>
Step 3 - Adesso vai a http://localhost:8080/index.php?r=site/about, il themes/basic/site/about.php verrà eseguito il rendering del file invece di views/site/about.php.
Step 4 - Per i moduli del tema, configurare la proprietà yii \ base \ Theme :: $ pathMap in questo modo.
'pathMap' => [
'@app/views' => '@app/themes/basic',
'@app/modules' => '@app/themes/basic/modules',
],
Step 5 - Per i widget del tema, configurare il file yii\base\Theme::$pathMap proprietà in questo modo.
'pathMap' => [
'@app/views' => '@app/themes/basic',
'@app/widgets' => '@app/themes/basic/widgets', // <-- !!!
],
A volte è necessario specificare un tema di base che contenga un aspetto e un aspetto di base dell'applicazione. Per raggiungere questo obiettivo, puoi utilizzare l'ereditarietà del tema.
Step 6 - Modifica il componente dell'applicazione di visualizzazione in questo modo.
'view' => [
'theme' => [
'basePath' => '@app/themes/basic',
'baseUrl' => '@web/themes/basic',
'pathMap' => [
'@app/views' => [
'@app/themes/christmas',
'@app/themes/basic',
],
]
],
],
Nella configurazione sopra, il @app/views/site/index.phpil file di visualizzazione avrà il tema @ app / temi / natale / site / index.php o @ app / temi / basic / site / index.php, a seconda del file esistente. Se esistono entrambi i file, verrà utilizzato il primo.
Step 7 - Crea il file themes/christmas/site struttura delle directory.
Step 8 - Ora, all'interno della cartella temi / natale / sito, crea un file chiamato about.php con il seguente codice.
<?php
/* @var $this yii\web\View */
use yii\helpers\Html;
$this->title = 'About'; $this->params['breadcrumbs'][] = $this->title; $this->registerMetaTag(['name' => 'keywords', 'content' => 'yii, developing,
views, meta, tags']);
$this->registerMetaTag(['name' => 'description', 'content' => 'This is the
description of this page!'], 'description');
?>
<div class = "site-about">
<h2>Christmas theme</h2>
<img src = "http://pngimg.com/upload/fir_tree_PNG2514.png" alt = ""/>
<p style = "color: red;">
This is the About page. You may modify the following file to customize its content:
</p>
</div>
Step 9 - Se vai a http://localhost:8080/index.php?r=site/about, vedrai la pagina delle informazioni aggiornata utilizzando il tema natalizio.