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';//在類中定義一個(gè)變量,名為$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為你的當(dāng)前控制器名,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!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

YiiassetbundlesorganizeandmanagewebassetslikeCSS,JavaScript,andimagesinaYiiapplication.1.Theysimplifydependencymanagement,ensuringcorrectloadorder.2.Theypreventduplicateassetinclusion.3.Theyenableenvironment-specifichandlingsuchasminification.4.Theyp

In the MVC framework, the mechanism for the controller to render views is based on the naming convention and allows explicit overwriting. If redirection is not explicitly indicated, the controller will automatically find a view file with the same name as the action for rendering. 1. Make sure that the view file exists and is named correctly. For example, the view path corresponding to the action show of the controller PostsController should be views/posts/show.html.erb or Views/Posts/Show.cshtml; 2. Use explicit rendering to specify different templates, such as render'custom_template' in Rails and view('posts.custom_template') in Laravel

When saving data to the database in the Yii framework, it is mainly implemented through the ActiveRecord model. 1. Creating a new record requires instantiation of the model, loading the data and verifying it before saving; 2. Updating the record requires querying the existing data before assignment; 3. When using the load() method for batch assignment, security attributes must be marked in rules(); 4. When saving associated data, transactions should be used to ensure consistency. The specific steps include: instantiating the model and filling the data with load(), calling validate() verification, and finally performing save() persistence; when updating, first obtaining records and then assigning values; when sensitive fields are involved, massassignment should be restricted; when saving the associated model, beginTran should be combined

TocreateabasicrouteinYii,firstsetupacontrollerbyplacingitinthecontrollersdirectorywithpropernamingandclassdefinitionextendingyii\web\Controller.1)Createanactionwithinthecontrollerbydefiningapublicmethodstartingwith"action".2)ConfigureURLstr

The method of creating custom operations in Yii is to define a common method starting with an action in the controller, optionally accept parameters; then process data, render views, or return JSON as needed; and finally ensure security through access control. The specific steps include: 1. Create a method prefixed with action; 2. Set the method to public; 3. Can receive URL parameters; 4. Process data such as querying the model, processing POST requests, redirecting, etc.; 5. Use AccessControl or manually checking permissions to restrict access. For example, actionProfile($id) can be accessed via /site/profile?id=123 and renders the user profile page. The best practice is

AYiidevelopercraftswebapplicationsusingtheYiiframework,requiringskillsinPHP,Yii-specificknowledge,andwebdevelopmentlifecyclemanagement.Keyresponsibilitiesinclude:1)Writingefficientcodetooptimizeperformance,2)Prioritizingsecuritytoprotectapplications,

AYiideveloper'skeyresponsibilitiesincludedesigningandimplementingfeatures,ensuringapplicationsecurity,andoptimizingperformance.QualificationsneededareastronggraspofPHP,experiencewithfront-endtechnologies,databasemanagementskills,andproblem-solvingabi

TouseActiveRecordinYiieffectively,youcreateamodelclassforeachtableandinteractwiththedatabaseusingobject-orientedmethods.First,defineamodelclassextendingyii\db\ActiveRecordandspecifythecorrespondingtablenameviatableName().Youcangeneratemodelsautomatic
