Yii2的相关学习记录,自定义gii模板和引用vendor中的js、css(四),yii2gii_PHP教程
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>
这样,再访问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>
方法二、用$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>
方法三、引入外部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>
定义此类后,在视图文件中:
backend\assets\AppAsset::register(<span>$this</span>);
即可将定义的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>);
先是注册了本来就有的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>
同时在layouts/main.php中:
backend\assets\Select2Asset::register(<span>$this</span><span>); </span><span>//</span><span> dmstr\web\AdminLteAsset::register($this);</span>
可以将AdminLteAsset这行注释掉,因为我们在Select2Asset中写明了依赖AdminLteAsset,所以会自动注册依赖文件的css和js,这时我们再次查看元素,就会发现多了select2.min.css和select2.min.js,有了这两个我们便可以使用三角样式的下拉菜单了。
以上就这样了,睡觉先。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool