上文中后台模板框架已经搭建起来了,但还是有些不协调,像是有两个User标题,或者我们想自己在gii生成时添加或删除些公用的东西。这就需要我们定义自己的gii模板。
我们以CRUD的模板为例,默认的gii模板位置是在:vendor/yiisoft/yii2-gii/generators/crud的default目录下,我们当然可以在此新建一个和default并列的目录,但是并不推荐在这里新建,因为这里是vendor目录,正如上章所说的,vendor目录下的东西尽量不要去更改,这样你在git发布时或团队共享时不需要提交vendor目录,比较方便。
正确的方法是,将default文件夹复制出来,移动到你想移动到的位置,我这里移动到了backend/views/gii/crud目录下,然后更改改backend/common/main-local.php下的gii配置:
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
'generators' => [ //here
'crud' => [ // generator name
'class' => 'yii\gii\generators\crud\Generator', // generator class
templates' => [ //setting for out templates
'vishun' => '@backend/views/gii/crud/default', // template name => path to template
]
]
],
];
这样,再访问gii中的CRUD生成器就会发现多了一个选择:(如果改的是backend的配置,那么只能通过后台的url访问gii才会出现多出来的选项)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoMAAADeCAIAAADJg53ZAAAgAElEQVR4nO2dTW8jyZ2n+Ynsbes7DObmi3URUNe+2ettEzA8Bw4KvvTMZTAEZj2NOawOludgqBprq2BjF1iUMJoBpxeYQ824urw9LpWa3dUtSkWJokiJ+bqHZGZGREYkM5mZiqT4PPgdpGRkxD9eMn4ZkUmpEwIAAIA9OrYDAAAA2GpwYgAAAJvgxAAAADbBiQEAAGyCEwMAANgEJwYAALAJTgwAAGATnBgAAMAmODEAAIBNcGIAAACb4MQAAAA2wYkBAABsghMDAADYpFEnPt3f24nY2z9dcXQzOO7tpBQM/7i30ztuOC4AANhYHsCJ9/b2RNdKjXjDnFhy4ZhVHhudhBMDAICJh3DiXm9PMKPjXnxIdGLBn2XfEj8QPzEdN65akxN6x1GiIqVn4khOik+JD2jKlUJM8jWXVTTCzOFlc6q5qnkAAEAbeQgn3j/eT634uLezs9Pb3xedOLvaNH6y/KD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kSuQmCC" alt="" />
记得要改拷贝过来的内容,例如删除掉多出来的标题什么的,然后重新生成一遍,就可以看到改变后的效果了。如果是团队合作,也应该将这个写入到环境中去。
再说下AdminLTE,我们在其官方演示站时,会发现模板集成了很多的漂亮功能,例如在Forms/Advanced Elements中select2,下拉菜单是箭头的样式。用firebug查看其css编发现引用了select2.min.css这个css。再回到我们自己的站点同样看下,便会发现我们站点没有这么一个css。所以我们要着手将其引入,但在此之前,需要先了解Yii2中如何在页面中引入js或者css。下面以引入js为例,css与此相同:
方法一、直接同在html中一样:(不建议使用,会导致依赖顺序错乱)
//外部js文件
<script src="你的js路径/plugins/select2/select2r.min.js">
//或者
<?=Html::jsFile('你的js路径/plugins/select2/select2r.min.js')?>
//内部js
<script> var a='abc' </script>
方法二、用$this->registerJs
和$this->registerJsFile
方法(可控依赖顺序,但是引入外部js时建议用方法三)
$this->registerJs("var options = ".json_encode($options).";", View::POS_END, 'my-options');//引入内部js
$this->registerJsFile('http://example.com/js/main.js', ['depends' => [\yii\web\JqueryAsset::className()]]);//引入外部js文件
方法三、引入外部js式时,利用Yii2新功能注册资源包来部署(功能更强大,例如可以转换less,压缩合并等),可参照backend/assets/AppAsset.php文件:
namespace backend\assets;
use yii\web\AssetBundle; class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/site.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
定义此类后,在视图文件中:
backend\assets\AppAsset::register($this);
即可将定义的site.css以及关联的'yii\web\YiiAsset'和'yii\bootstrap\BootstrapAsset'定义的css和js引入。更多功能请看YII2权威指南中的“客户端脚本”和“资源”两个章节。
既然已了解以上引入方法,可以继续我们引入select2的计划,我们先看下我们站点引入了哪些资源,在你copy后的layouts/main.php中:
if (class_exists('backend\assets\AppAsset')) {
backend\assets\AppAsset::register($this);
} else {
app\assets\AppAsset::register($this);
}
dmstr\web\AdminLteAsset::register($this);
先是注册了本来就有的AppAsset,然后又注册了dmstr\web命名空间下的AdminLteAsset,我们追溯到此文件下就会发现里面只引入了vendor/almasaeed2010/dist文件中的AdminLTE.min.css和变动的*.min.css这两个css文件(almasaeed2010文件是我们compose下载yii2-adminlte-asset自动下载的依赖文件),但我们打开vendor/almasaeed2010/adminlte/plugins下,会发现还有很多的功能,我们上方提到的select2也在其中。我们当然可以在AdminLteAsset.php的平级建立Select2Asset.php文件,但理由同上方gii模板时说的,最好不要改动vendor下的文件,所以我们将此文件建立在backend/assets中,具体文件内容可参考:
<?php
namespace backend\assets;
use yii\web\AssetBundle; class Select2Asset extends AssetBundle
{
public $sourcePath = '@vendor/almasaeed2010/adminlte/plugins/select2';//路径
public $css = [
'select2.min.css',//css
];
public $js = [
'select2.min.js'//js
];
public $depends = [
'dmstr\web\AdminLteAsset',//依赖关系
];
}
同时在layouts/main.php中:
backend\assets\Select2Asset::register($this);
// dmstr\web\AdminLteAsset::register($this);
可以将AdminLteAsset这行注释掉,因为我们在Select2Asset中写明了依赖AdminLteAsset,所以会自动注册依赖文件的css和js,这时我们再次查看元素,就会发现多了select2.min.css和select2.min.js,有了这两个我们便可以使用三角样式的下拉菜单了。
以上就这样了,睡觉先。