国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
What Exactly Is an Asset Bundle?
How to Register an Asset Bundle
Where to Put Your Assets
Versioning and Cache Busting
Home PHP Framework YII How do I use asset bundles in Yii?

How do I use asset bundles in Yii?

Jul 08, 2025 am 12:33 AM
yii

Using Yii's asset bundles is a best practice for managing CSS and JS files. It defines resource groups centrally through PHP classes, and automatically handles dependencies, merging and caches. 1. The resource package is a PHP class used to organize resources such as CSS, JS and declare their dependencies; 2. Register resource packages in the view or layout to automatically generate HTML tags; 3. Different resource packages can be conditionally registered according to user roles or page types; 4. Place resource files in web/css and web/js by default, and you can also customize the path; 5. Use the assetManager configuration to add timestamps to implement version control, solving browser caching problems. Correct use of resource packages can improve project structure clarity and loading efficiency.

How do I use asset bundles in Yii?

Using asset bundles in Yii is one of the best ways to manage your CSS and JS files. It helps keep things organized, versioned, and efficient — especially as your project grows. The main idea is that instead of manually adding script or style tags in your views, you define everything through PHP classes.

What Exactly Is an Asset Bundle?

An asset bundle is just a PHP class that groups related assets like CSS, JS, images, or even other resources. Each bundle defines which files to load and any dependencies it has (like jQuery or Bootstrap). Yii handles the rest — combining, publishing, and caching them appropriately.

For example:

 class AppAsset extends AssetBundle {
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
    ];
    public $js = [
        'js/main.js',
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap5\BootstrapAsset',
    ];
}

This simple structure tells Yii where to find the files, what order to load them in, and how they relate to other bundles.

How to Register an Asset Bundle

Registering a bundle is straightforward. You do this either in a view file or in a layout file, depending on how widely you want it used.

In a layout ( @app/views/layouts/main.php , for example):

 use app\assets\AppAsset;
AppAsset::register($this);

That's all. Once registered, Yii will output the correct <link> and <script> tags automatically when the page renders. If you're using different layouts or partial views, register the bundle there too — just make sure not to duplicate it unnecessarily.

You can also conditionally register bundles based on user roles or page types. For example:

  • Load admin-specific styles only in the backend
  • Use mobile-optimized scripts only when detected

Where to Put Your Assets

By default, most people put their CSS and JS under web/css and web/js . But you can change this by setting $basePath and $baseUrl in your bundle.

If you have a more complex setup — like frontend and backend separated — consider creating separate bundles for each part of your app. For example:

  • FrontendAsset
  • BackendAsset
  • LoginAsset (for login-only styles)

Also, if you use SCSS or TypeScript, don't forget to compile them first and place the resulting files under web/assets/compiled/ or similar. Yii won't process those on its own.

Versioning and Cache Busting

One common gotcha: browsers cache static files aggressively. So after deployment, users might still be loading old CSS or JS.

To fix that, Yii lets you add a hash or timestamp to your published assets. This is usually done via the assetManager component in your config:

 &#39;assetManager&#39; => [
    &#39;appendTimestamp&#39; => true,
],

With that, every time a file changes, the URL changes too — forcing the browser to fetch the new version.

Another option is to use versioned symbolic links, but that's more advanced and typically only needed for high-traffic sites.


That's the core of working with asset bundles in Yii. It takes a bit of setup, but once you get the hang of it, managing static assets become much smoother. Just remember to organize your bundles by usage, register them in the right places, and handle versioning so users always get the latest code.

The above is the detailed content of How do I use asset bundles in Yii?. 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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

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)

Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Jun 19, 2023 am 08:09 AM

In the current information age, big data, artificial intelligence, cloud computing and other technologies have become the focus of major enterprises. Among these technologies, graphics card rendering technology, as a high-performance graphics processing technology, has received more and more attention. Graphics card rendering technology is widely used in game development, film and television special effects, engineering modeling and other fields. For developers, choosing a framework that suits their projects is a very important decision. Among current languages, PHP is a very dynamic language. Some excellent PHP frameworks such as Yii2, Ph

Data query in Yii framework: access data efficiently Data query in Yii framework: access data efficiently Jun 21, 2023 am 11:22 AM

The Yii framework is an open source PHP Web application framework that provides numerous tools and components to simplify the process of Web application development, of which data query is one of the important components. In the Yii framework, we can use SQL-like syntax to access the database to query and manipulate data efficiently. The query builder of the Yii framework mainly includes the following types: ActiveRecord query, QueryBuilder query, command query and original SQL query

How to use Yii3 framework in php? How to use Yii3 framework in php? May 31, 2023 pm 10:42 PM

As the Internet continues to develop, the demand for web application development is also getting higher and higher. For developers, developing applications requires a stable, efficient, and powerful framework, which can improve development efficiency. Yii is a leading high-performance PHP framework that provides rich features and good performance. Yii3 is the next generation version of the Yii framework, which further optimizes performance and code quality based on Yii2. In this article, we will introduce how to use Yii3 framework to develop PHP applications.

Symfony vs Yii2: Which framework is better for developing large-scale web applications? Symfony vs Yii2: Which framework is better for developing large-scale web applications? Jun 19, 2023 am 10:57 AM

As the demand for web applications continues to grow, developers have more and more choices in choosing development frameworks. Symfony and Yii2 are two popular PHP frameworks. They both have powerful functions and performance, but when faced with the need to develop large-scale web applications, which framework is more suitable? Next we will conduct a comparative analysis of Symphony and Yii2 to help you make a better choice. Basic Overview Symphony is an open source web application framework written in PHP and is built on

How to use PHP framework Yii to develop a highly available cloud backup system How to use PHP framework Yii to develop a highly available cloud backup system Jun 27, 2023 am 09:04 AM

With the continuous development of cloud computing technology, data backup has become something that every enterprise must do. In this context, it is particularly important to develop a highly available cloud backup system. The PHP framework Yii is a powerful framework that can help developers quickly build high-performance web applications. The following will introduce how to use the Yii framework to develop a highly available cloud backup system. Designing the database model In the Yii framework, the database model is a very important part. Because the data backup system requires a lot of tables and relationships

What is the difference between php framework laravel and yii What is the difference between php framework laravel and yii Apr 30, 2025 pm 02:24 PM

The main differences between Laravel and Yii are design concepts, functional characteristics and usage scenarios. 1.Laravel focuses on the simplicity and pleasure of development, and provides rich functions such as EloquentORM and Artisan tools, suitable for rapid development and beginners. 2.Yii emphasizes performance and efficiency, is suitable for high-load applications, and provides efficient ActiveRecord and cache systems, but has a steep learning curve.

Yii2 vs Symfony: Which framework is better for API development? Yii2 vs Symfony: Which framework is better for API development? Jun 18, 2023 pm 11:00 PM

With the rapid development of the Internet, APIs have become an important way to exchange data between various applications. Therefore, it has become increasingly important to develop an API framework that is easy to maintain, efficient, and stable. When choosing an API framework, Yii2 and Symfony are two popular choices among developers. So, which one is more suitable for API development? This article will compare these two frameworks and give some conclusions. 1. Basic introduction Yii2 and Symfony are mature PHP frameworks with corresponding extensions that can be used to develop

Yii with Docker: Containerizing and Deploying Your Applications Yii with Docker: Containerizing and Deploying Your Applications Apr 02, 2025 pm 02:13 PM

The steps to containerize and deploy Yii applications using Docker include: 1. Create a Dockerfile and define the image building process; 2. Use DockerCompose to launch Yii applications and MySQL database; 3. Optimize image size and performance. This involves not only specific technical operations, but also understanding the working principles and best practices of Dockerfile to ensure efficient and reliable deployment.

See all articles