Table of Contents
Yii2的相关学习记录,自定义gii模板和引用vendor中的js、css(四),yii2gii
Home php教程 php手册 Yii2的相关学习记录,自定义gii模板和引用vendor中的js、css(四),yii2gii

Yii2的相关学习记录,自定义gii模板和引用vendor中的js、css(四),yii2gii

Jun 13, 2016 am 08:43 AM
css gii yii2 and study Quote template of Related customize Record

Yii2的相关学习记录,自定义gii模板和引用vendor中的js、css(四),yii2gii

上文中后台模板框架已经搭建起来了,但还是有些不协调,像是有两个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配置:

<span>$config</span>['bootstrap'][] = 'gii'<span>;
</span><span>$config</span>['modules']['gii'] =<span> [
    </span>'class' => 'yii\gii\Module',
    'generators' => [ <span>//</span><span>here</span>
        'crud' => [ <span>//</span><span> generator name</span>
            'class' => 'yii\gii\generators\crud\Generator', <span>//</span><span> generator class</span>
            templates'<span> => [ //setting for out templates
                </span>'vishun' => '@backend/views/gii/crud/<span>default</span>'<span>, // template name => path to template
            ]
        ]
    ],
];    </span>
Copy after login

这样,再访问gii中的CRUD生成器就会发现多了一个选择:(如果改的是backend的配置,那么只能通过后台的url访问gii才会出现多出来的选项)

记得要改拷贝过来的内容,例如删除掉多出来的标题什么的,然后重新生成一遍,就可以看到改变后的效果了。如果是团队合作,也应该将这个写入到环境中去。

再说下AdminLTE,我们在其官方演示站时,会发现模板集成了很多的漂亮功能,例如在Forms/Advanced Elements中select2,下拉菜单是箭头的样式。用firebug查看其css编发现引用了select2.min.css这个css。再回到我们自己的站点同样看下,便会发现我们站点没有这么一个css。所以我们要着手将其引入,但在此之前,需要先了解Yii2中如何在页面中引入js或者css。下面以引入js为例,css与此相同:

方法一、直接同在html中一样:(不建议使用,会导致依赖顺序错乱)

<span>//</span><span>外部js文件</span>
<script src="你的js路径/plugins/select2/select2r.min.js">
<span>//</span><span>或者</span>
<?=Html::jsFile('你的js路径/plugins/select2/select2r.min.js')?>
<span>//</span><span>内部js</span>
<script> <span>var</span> a='abc' </script>
Copy after login

方法二、用$this->registerJs$this->registerJsFile方法(可控依赖顺序,但是引入外部js时建议用方法三)

<span>$this</span>->registerJs("var options = ".json_encode(<span>$options</span>).";", View::POS_END, 'my-options');<span>//</span><span>引入内部js</span>
<span>$this</span>->registerJsFile('http://example.com/js/main.js', ['depends' => [\yii\web\JqueryAsset::className()]]);<span>//</span><span>引入外部js文件</span>
Copy after login

方法三、引入外部js式时,利用Yii2新功能注册资源包来部署(功能更强大,例如可以转换less,压缩合并等),可参照backend/assets/AppAsset.php文件:

<span>namespace backend\assets;
</span><span>use</span><span> yii\web\AssetBundle;

</span><span>class</span> AppAsset <span>extends</span><span> AssetBundle
{
    </span><span>public</span> <span>$basePath</span> = '@webroot'<span>;
    </span><span>public</span> <span>$baseUrl</span> = '@web'<span>;
    </span><span>public</span> <span>$css</span> =<span> [
        </span>'css/site.css',<span>
    ];
    </span><span>public</span> <span>$js</span> =<span> [
    ];
    </span><span>public</span> <span>$depends</span> =<span> [
        </span>'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',<span>
    ];
}</span>
Copy after login

定义此类后,在视图文件中:

backend\assets\AppAsset::register(<span>$this</span>);
Copy after login

即可将定义的site.css以及关联的'yii\web\YiiAsset'和'yii\bootstrap\BootstrapAsset'定义的css和js引入。更多功能请看YII2权威指南中的“客户端脚本”和“资源”两个章节。

既然已了解以上引入方法,可以继续我们引入select2的计划,我们先看下我们站点引入了哪些资源,在你copy后的layouts/main.php中:

<span>if</span> (<span>class_exists</span>('backend\assets\AppAsset'<span>)) {
    backend\assets\AppAsset</span>::register(<span>$this</span><span>);
} </span><span>else</span><span> {
    app\assets\AppAsset</span>::register(<span>$this</span><span>);
}
dmstr\web\AdminLteAsset</span>::register(<span>$this</span>);
Copy after login

先是注册了本来就有的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中,具体文件内容可参考:

<?<span>php
namespace backend\assets;
</span><span>use</span><span> yii\web\AssetBundle;

</span><span>class</span> Select2Asset <span>extends</span><span> AssetBundle
{
    </span><span>public</span> <span>$sourcePath</span> = '@vendor/almasaeed2010/adminlte/plugins/select2';<span>//</span><span>路径</span>
    <span>public</span> <span>$css</span> =<span> [
        </span>'select2.min.css',<span>//</span><span>css</span>
<span>    ];
    </span><span>public</span> <span>$js</span> =<span> [
        </span>'select2.min.js'<span>//</span><span>js</span>
<span>    ];
    </span><span>public</span> <span>$depends</span> =<span> [
        </span>'dmstr\web\AdminLteAsset',<span>//</span><span>依赖关系</span>
<span>    ];
}</span>
Copy after login

同时在layouts/main.php中:

backend\assets\Select2Asset::register(<span>$this</span><span>);
</span><span>//</span><span> dmstr\web\AdminLteAsset::register($this);</span>
Copy after login

可以将AdminLteAsset这行注释掉,因为我们在Select2Asset中写明了依赖AdminLteAsset,所以会自动注册依赖文件的css和js,这时我们再次查看元素,就会发现多了select2.min.css和select2.min.js,有了这两个我们便可以使用三角样式的下拉菜单了。

以上就这样了,睡觉先。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

See all articles