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!

This tutorial demonstrates Yii Framework's timestamp management. It details using TimestampBehavior for automatic created_at and updated_at updates, offering customization options and comparing it to manual updates, database triggers, and custom be

The article discusses best practices for deploying Yii applications in cloud-native environments, focusing on scalability, reliability, and efficiency through containerization, orchestration, and security measures.

This article compares the PHP frameworks Yii and Laravel. Yii prioritizes speed and structure, while Laravel emphasizes developer experience and flexibility. Though both handle large-scale applications, Yii offers superior raw performance, while La

This article introduces Yii, a high-performance PHP framework ideal for large-scale web applications. It highlights Yii's speed, security, and robust architecture (MVC), emphasizing its advantages over other frameworks like Laravel, Symfony, and Cod

This article analyzes Yii framework's strengths and weaknesses. It highlights Yii's high performance, robust security, rapid development capabilities, and extensibility, but also notes a steeper learning curve and potential complexity for smaller pr

This article compares Yii and ThinkPHP (TP) frameworks. The choice depends on project scale and developer experience. Yii, robust and mature, suits large, complex projects needing high performance. TP, simpler and faster for development, is better f

This article details how to call and organize common functions in Yii applications. It advocates encapsulating functions within classes, ideally in a dedicated app/helpers directory, for improved reusability and maintainability. Different approache

The article discusses key considerations for using Yii in serverless architectures, focusing on statelessness, cold starts, function size, database interactions, security, and monitoring. It also covers optimization strategies and potential integrati


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

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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

Dreamweaver CS6
Visual web development tools

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