Home PHP Framework YII How to introduce CSS and JS files in Yii2 framework

How to introduce CSS and JS files in Yii2 framework

Dec 18, 2019 pm 02:59 PM
yii2

How to introduce CSS and JS files in Yii2 framework

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;
Copy after login

Then you can call it like this in the following Html

<?=Html::jsFile(&#39;@web/***/js/***.js&#39;)?>//这里***代表你的目录名或者文件名
<?=Html::cssFile(&#39;@web/***/css/***.css&#39;)?>//***同上
Copy after login

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 = &#39;layout&#39;;//在类中定义一个变量,名为$layout
Copy after login

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; ?>
Copy after login

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;
Copy after login

Then write this where you need the link to jump:

<?phpecho Url::toRoute(&#39;post/index&#39;);?>//post为你的当前控制器名,index为view模版
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What Are the Best Practices for Using Yii in a Cloud-Native Environment? What Are the Best Practices for Using Yii in a Cloud-Native Environment? Mar 18, 2025 pm 04:39 PM

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.

What Are the Key Considerations for Using Yii in a Serverless Architecture? What Are the Key Considerations for Using Yii in a Serverless Architecture? Mar 18, 2025 pm 04:33 PM

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

What Are the Best Strategies for Testing Yii Applications with Codeception? What Are the Best Strategies for Testing Yii Applications with Codeception? Mar 18, 2025 pm 04:27 PM

The article discusses strategies for testing Yii applications using Codeception, focusing on using built-in modules, BDD, different test types, mocking, CI integration, and code coverage.

What Are the Key Features of Yii's Built-in Testing Framework? What Are the Key Features of Yii's Built-in Testing Framework? Mar 18, 2025 pm 04:41 PM

Yii's built-in testing framework enhances application testing with features like PHPUnit integration, fixture management, and support for various test types, improving code quality and development practices.

How to Implement Real-Time Data Synchronization with Yii and WebSockets? How to Implement Real-Time Data Synchronization with Yii and WebSockets? Mar 18, 2025 pm 04:34 PM

The article discusses implementing real-time data synchronization using Yii and WebSockets, covering setup, integration, and best practices for performance and security.

What Are the Key Benefits of Using Yii for Building SaaS Applications? What Are the Key Benefits of Using Yii for Building SaaS Applications? Mar 18, 2025 pm 04:25 PM

The article discusses Yii's benefits for SaaS development, focusing on performance, security, and rapid development features to enhance scalability and reduce time-to-market.

How to Build a Real-Time Geolocation Tracking System with Yii? How to Build a Real-Time Geolocation Tracking System with Yii? Mar 18, 2025 pm 04:40 PM

Article discusses building real-time geolocation tracking with Yii, covering setup, database design, and security. Main focus is on integration and best practices for data privacy and security.

How to Implement Service Discovery and Load Balancing in Yii Microservices? How to Implement Service Discovery and Load Balancing in Yii Microservices? Mar 18, 2025 pm 04:30 PM

The article discusses implementing service discovery and load balancing in Yii microservices, detailing steps and best practices for efficient communication and workload distribution.

See all articles