Yii2.0 introduces CSS and JS file methods

Source: Internet
Author: User
In yii2, the yii2 version upgrade has resulted in many yii2 usage differences from yii1. in the past few days, the view interface on The view layer has been wandering. What problems have you encountered? The problem is that I cannot figure out how to introduce CSS. in yii2, the JS file is upgraded to yii2. many yii2 usage is quite different from that of yii1, in the past few days, the view interface on The view layer has been hovering around. What problems have you encountered? The problem is that I cannot figure out how to introduce CSS and JS files! I also read the tutorials from other experienced experts in the community, but there are still some problems. for example, after the yii2 project is opened, the headers and tails are public, how to remove it? And how to avoid modifying the original main. in the case of PHP files, to introduce JS and CSS files, there may be a way to write a xxxAsset. the php configuration file can be introduced through xxx Asset: register ($ this), but now I have encountered a problem. this code is invalid and does not work, after firebug was opened, no CSS or JS files were introduced into the head, and the style became messy. later, I checked the relevant information, I wrote a yii2 background template for another user. so this morning I checked how the background style was laid out. I summarized the following:
1. in the front-end view, the simplest thing is the introduction of a file and a file as before. Therefore, use at the top to call the code segment.

Use yii \ helpers \ Html;

Then, you can call

 // Here *** represents your directory name or file name
 // *** Same as above

In this case, you don't need to move other files. just introduce the file directly. which one should introduce the file? of course, you need to write a lot of lines of code to load each time. you 'd better write it to the configuration file, however, I haven't gotten through this problem by using the configuration file. I will share it with you if I find the cause later.
2. how can I customize a style file in the controller when the foreground is introduced like this?
Add the following code to the controller:

Public $ layout = 'layout '; // defines a variable named $ layout in the class.

Note that this layout has a directory named layouts in your view. under this directory, I created a new file named layout. php, where I added a code

In this way, the controller will automatically find the php file for loading the view file under the layouts directory under the current view directory.
The above few lines of short code solve the problem that beginners do not know how to load CSS and JS files. if you think that writing *** Asset. php files may be faulty. I will use this method. later I will use other methods to load php files after I become familiar with yii2.
In addition, I would like to add how to jump to other view files in the view
Introduce the class library at the top.

Use yii \ helpers \ Url;

Then you need to write the following link jump:

// Post your current controller name and index is the view template

Isn't that easy!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.