Yii主題化

主題化可以幫助你更換一組視圖,而不需要修改原來的視圖文件。應該設置視圖應用程序組件的 theme 屬性來使用主題。

也應該定義以下屬性 -

  • yii\base\Theme::$basePath − 定義 CSS, JS, images 的基本目錄等等

  • yii\base\Theme::$baseUrl − 定義主題資源的基本URL

  • yii\base\Theme::$pathMap − 定義替換規則

例如,如果在 UserController 中調用 $this->render('create') ,  @app/views/user/create.php 視圖文件將被渲染。

如果下面的應用配置主題化,視圖文件  @app/themes/basic/user/create.php 將渲染代替。

第1步 - 修改 config/web.php 文件使用以下代碼。

'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' => 'sfdsajkljfkldsajkfldsa-yiibai.com-dsaf ', \], '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' => \[ 'admin' => \[ 'class' => 'app\\modules\\admin\\Admin', \], \], '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; ?>

我們已經添加了視圖應用程序組件。

第2步 - 現在創建了  web/themes/basic 和  themes/basic/site 目錄結構。在  themes/basic/site 文件夾裏面創建一個 about.php 文件並使用下面的代碼。

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'); ?>

title) ?>

這是一個「關於頁面」,現在正在使用的視圖文件是:

第3步 - 現在,訪問  http://localhost:8080/index.php?r=site/about ,themes/basic/site/about.php 文件將被渲染,而不是渲染 views/site/about.php 文件。
Yii主題化

第4步 - 到 主題模塊,使用以下方式配置  yii\base\Theme::$pathMap 。

'pathMap' => [
'@app/views' => '@app/themes/basic',
'@app/modules' => '@app/themes/basic/modules',
],

第5步 - 到 widgets 模塊,使用以下方式配置  yii\base\Theme::$pathMap 。

'pathMap' => [
'@app/views' => '@app/themes/basic',
'@app/widgets' => '@app/themes/basic/widgets', // <-- !!!-->
],

有時需要指定一個基本主題,它包含應用程序的基本外觀。爲了實現這個目標,可以使用主題繼承。

第6步 - 修改視圖(view)應用程序組件,並使用以下代碼。

'view' => [
'theme' => [
'basePath' => '@app/themes/basic',
'baseUrl' => '@web/themes/basic',
'pathMap' => [
'@app/views' => [
'@app/themes/newyear',
'@app/themes/basic',
],
]
],
],

在上面的配置中,@app/views/site/index.php 或 @app/themes/newyear/site/index.php 或  @app/themes/basic/site/index.php 視圖文件將被作爲主題,這取決於哪些文件存在。如果兩個文件都存在,第一個將被使用。

第7步 - 創建  themes/newyear/site 目錄結構。

第8步 - 現在  themes/newyear/site 文件夾內,創建一個 about.php 文件並用下面的代碼。

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'); ?>

New Year Theme

新年主題

這是一個「關於頁面」,現在正在使用的視圖文件是:

第9步 - 打開URL=>  http://localhost:8080/index.php?r=site/about ,就會看到更新的有關使用新年的主題頁面。
Yii主題化