在 Yii2 Starter Kit 的后台应用中,新建一套主题的流程
1、新建目录:\backend\views\themes\default,指定包含主题资源(CSS, JS, images, 等等)的基准目录,如图12、复制目录:\backend\views\ 下的所有文件(除去themes),如图23、粘贴至目录:\backend\views\themes\default\ 下,已经复制成功,如图34、配置 view 应用组件的 [[yii\base\View::theme|theme]] 属性,如图45、配置 Gii 模板文件,其主题为:default,如图56、主题化模块,如:i18n ,新建目录:\backend\modules\i18n\views\themes\default,如图67、复制目录:\backend\modules\i18n\views\ 下的所有文件(除去themes),如图78、粘贴至目录:\backend\modules\i18n\views\themes\default\ 下,已经复制成功,如图89、配置 view 应用组件的 [[yii\base\View::theme|theme]] 属性,如图910、通过 [[yii\base\View::theme]] 属性访问 [[yii\base\Theme]] 对象。编辑:\backend\views\themes\default\sign-in\login.php,如图1011、打印出的对象,如图11
yii\base\Theme Object
(
[pathMap] => Array
(
[@app/views] => @app/views/themes/default
[@app/modules/i18n/views] => @app/modules/i18n/views/themes/default
)
[_baseUrl:yii\base\Theme:private] => /views/themes/default
[_basePath:yii\base\Theme:private] => E:\wwwroot\cmcp\backend/views/themes/default
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
)12、在 .env 中设置主题名称:default,如图1213、设置主题相关别名,编辑:\backend\config\bootstrap.php,定义别名,如图1314、第5步骤的设置,调整,如图1415、第9步骤的设置,调整,如图1516、新建目录:\backend\web\themes\default,指定包含主题资源(CSS, JS, images, 等等)的基准目录,如图1617、复制目录:\backend\web\ 下的目录(css、img、js),如图1718、粘贴至目录:\backend\web\themes\default\ 下,已经复制成功,如图1819、基于主题重新定义资源包,编辑:\backend\assets\BackendAsset.php,如图1920、查看登录页面的源代码,最大的变化在于包含的CSS/JS文件,其路径为绝对路径,如图20
'components' => [
'view' => [
'theme' => [
'basePath' => '@app/views/themes/default',
'baseUrl' => '@web/views/themes/default',
'pathMap' => [
'@app/views' => '@app/views/themes/default',
],
],
],
],
if (YII_ENV_DEV) {
$config['modules']['gii'] = [
'class' => yii\gii\Module::class,
'generators' => [
'crud' => [
'class' => yii\gii\generators\crud\Generator::class,
'templates' => [
'yii2-starter-kit' => Yii::getAlias('@backend/views/themes/default/_gii/templates')
],
'template' => 'yii2-starter-kit',
'messageCategory' => 'backend'
]
]
];
}
'view' => [
'theme' => [
'basePath' => '@app/views/themes/default',
'baseUrl' => '@web/views/themes/default',
'pathMap' => [
'@app/views' => '@app/views/themes/default',
'@app/modules/i18n/views' => '@app/modules/i18n/views/themes/default',
],
],
],
$theme = $this->theme;
print_r($theme);
exit;
# Themes
# ----
BACKEND_THEME = default
/**
* Setting theme aliases
*/
Yii::setAlias('@themePath', '@backend/web/themes/' . env('BACKEND_THEME'));
Yii::setAlias('@themeUrl', '@backendUrl/themes/' . env('BACKEND_THEME'));
if (YII_ENV_DEV) {
$config['modules']['gii'] = [
'class' => yii\gii\Module::class,
'generators' => [
'crud' => [
'class' => yii\gii\generators\crud\Generator::class,
'templates' => [
'yii2-starter-kit' => Yii::getAlias('@backend/views/themes/' . env('BACKEND_THEME') . '/_gii/templates')
],
'template' => 'yii2-starter-kit',
'messageCategory' => 'backend'
]
]
];
}
'view' => [
'theme' => [
'basePath' => '@app/views/themes/' . env('BACKEND_THEME'),
'baseUrl' => '@web/views/themes/' . env('BACKEND_THEME'),
'pathMap' => [
'@app/views' => '@app/views/themes/' . env('BACKEND_THEME'),
'@app/modules/i18n/views' => '@app/modules/i18n/views/themes/' . env('BACKEND_THEME'),
],
],
],
public $basePath = '@themePath';
public $baseUrl = '@themeUrl';



![配置 view 应用组件的 [[yii\base\View::theme|theme]] 属性](https://www.shuijingwanwq.com/wp-content/uploads/2017/11/4-2.png)




![配置 view 应用组件的 [[yii\base\View::theme|theme]] 属性](https://www.shuijingwanwq.com/wp-content/uploads/2017/11/9.png)
![通过 [[yii\base\View::theme]] 属性访问 [[yii\base\Theme]] 对象。编辑:\backend\views\themes\default\sign-in\login.php](https://www.shuijingwanwq.com/wp-content/uploads/2017/11/10.png)









