In yii2, due to the upgrade of yii2 version, many usages of yii2 are very different from yii1. I have been wandering around the view interface of the view layer for the past few days and encountered What's the problem? (Recommended learning: yii framework )
The question is that I can't figure out how I should introduce CSS, js file! I also read the tutorials of other experienced masters in the community and followed them, but there are still some problems. For example, after the yii2 project is opened, the header and tail are public. How to remove them?
And how to introduce JS and CSS files without changing the original main.php file. Maybe one way is to write a configuration file of xxxAsset.php, and then pass xxx Asset::register($ this) can import files, but now I encountered a problem again. When I encountered this code, it was invalid and did not work. After opening firebug, no CSS or JS files were imported into the head, and the styles became messy.
Later I checked the relevant information, and coincidentally, I downloaded a yii2 backend template posted by someone else. So, this morning I took a look at how the backend style is laid out and summarized it:
1. The simplest thing in the front view is to introduce files one by one as before, so use use at the top to call the code segment
use yii\helpers\Html;
Then you can call it like this in the following Html
<?=Html::jsFile('@web/***/js/***.js')?>//这里***代表你的目录名或者文件名 <?=Html::cssFile('@web/***/css/***.css')?>//***同上
In this case, you don’t need to touch other files, just import the file directly. Which one needs to be imported, of course write like this If so, you have to write many lines of code to load each time. It is best to write it in the configuration file. But I haven’t figured out how to use the configuration file to introduce this problem yet. If I find the reason later, I will share it with you
2. The front desk is introduced in this way, so how to customize the style file in the controller? Add the following code in the controller
public $layout = 'layout';//在类中定义一个变量,名为$layout
Note that this layout has a directory called layouts in your view , in this directory, I created a new file named layout.php, in which I added a code
<?php echo $content; ?>
so that the controller will automatically find the loading view in the layouts directory under the current view directory. The few lines of short code above the php file of the file solve the problem of novices not knowing how to load CSS and JS files. If you think there will be problems writing ***Asset.php files, use my method. Later, after I became familiar with yii2, I switched to other methods to load. In addition, I will add that how to jump and link to other view files in the view is also introduced at the top.
use yii\helpers\Url;
Then write this where you need the link to jump:
<?phpecho Url::toRoute('post/index');?>//post为你的当前控制器名,index为view模版
The above is the detailed content of How to introduce CSS and JS files in Yii2 framework. For more information, please follow other related articles on the PHP Chinese website!