


Analysis on the use of the front-end resource package that comes with the Yii framework in PHP
This article mainly introduces the use of front-end resource packages in PHP's Yii framework, and lists some commonly used JavaScript and CSS resources in Yii. Friends in need can refer to the following
The resources in Yii are and Files related to Web pages can be CSS files, JavaScript files, pictures or videos, etc. The resources are placed in a directory accessible to the Web and are directly called by the Web server.
It is better to automatically manage resources through programs. For example, when you use the yii\jui\DatePicker widget in a page, it will automatically include the required CSS and JavaScript files instead of requiring you to find them manually. These files are also included. When you upgrade the widget, it will automatically use the new version of the resource files. In this tutorial, we will detail the powerful resource management functions provided by Yii.
Resource package
Yii manages resources in resource packages. Resource packages are simply a collection of resources placed in a directory. When a resource is registered in a view Package, the CSS and JavaScript files in the package will be included when rendering the web page.
Define resource package
The resource package is specified as a PHP class that inherits yii\web\AssetBundle. The package name is the PHP class name that can be automatically loaded. In the resource package class , you need to specify the location of the resource, which CSS and JavaScript files it contains, and its dependencies on other packages.
The following code defines the main resource package used by the basic application template:
<?php namespace app\assets; use yii\web\AssetBundle; class AppAsset extends AssetBundle { public $basePath = '@webroot'; public $baseUrl = '@web'; public $css = [ 'css/site.css', ]; public $js = [ ]; public $depends = [ 'yii\web\YiiAsset', 'yii\bootstrap\BootstrapAsset', ]; }
As above, the resource files specified by the AppAsset class are placed in the @webroot directory. The corresponding URL is @web. The resource bundle contains a CSS file css/site.css. There is no JavaScript file. It depends on the other two packages yii\web\YiiAsset and yii\bootstrap\BootstrapAsset. About the properties of yii\web\AssetBundle More details are described below:
yii\web\AssetBundle::sourcePath: Specifies the root directory of the bundle containing resource files. This property should be set when the root directory cannot be accessed by the Web. Otherwise, the yii\web\AssetBundle::basePath property and yii\web\AssetBundle::baseUrl should be set. Path aliases can be used here;
yii\web\AssetBundle::basePath: specifies the directory that contains the resource files in the resource bundle and can be accessed by the Web. When specifying yii\web\AssetBundle ::sourcePath attribute, the resource manager will publish the package's resources to a Web-accessible directory and override this attribute. If your resource file is in a Web-accessible directory, you should set this attribute so that you do not need to publish it again. Path aliases can be used here.
yii\web\AssetBundle::baseUrl: Specifies the URL corresponding to the yii\web\AssetBundle::basePath directory, similar to yii\web\AssetBundle::basePath. If you specify yii\web\AssetBundle::sourcePath property, the asset manager will publish these resources and override this property, path aliases can be used here.
yii\web\AssetBundle::js: An array containing the JavaScript files of the resource bundle. Note that the forward slash "/" should be used as the directory separator. Each JavaScript file can be specified in one of the following two formats:
The relative path is expressed as a local JavaScript file (such as js/main.js). The actual path of the file is preceded by yii\web\AssetManager::basePath. The actual path of the file is The URL has yii\web\AssetManager::baseUrl in front of the path.
Absolute URL address is represented as an external JavaScript file, such as http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js or // ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js.
yii\web\AssetBundle::css: A JavaScript file containing the resource bundle Array, the array format is the same as yii\web\AssetBundle::js.
yii\web\AssetBundle::depends: A list of other resource bundles that this resource bundle depends on (detailed introduction in the next two sections).
yii\web\AssetBundle::jsOptions: When calling yii\web\View::registerJsFile() to register each JavaScript file in the bundle, specify the options passed to this method.
yii\web\AssetBundle::cssOptions: When calling yii\web\View::registerCssFile() to register each css file in the bundle, specify the options passed to this method.
yii\web\AssetBundle::publishOptions: Specify the options passed to this method when calling yii\web\AssetManager::publish() to publish the package resource file to the Web directory. Only used when the yii\web\AssetBundle::sourcePath attribute is specified.
Resource location
Resources can be divided into:
Source resources: resource files and PHP source code according to their location Put together, they cannot be directly accessed by the Web. In order to use these source resources, they must be copied to a Web directory that can be accessed by the Web and become published resources. This process is called publishing resources, which will be introduced in detail later.
Publish resources: The resource files are placed in a Web directory that can be directly accessed through the Web;
External resources: The resource files are placed on a different Web server from your Web application;
When defining the resource package class, If you specify the yii\web\AssetBundle::sourcePath attribute, it means that any resources using relative paths will be regarded as source resources; if you do not specify this attribute, it means that these resources are published resources (therefore you should specify yii\web\ AssetBundle::basePath and yii\web\AssetBundle::baseUrl let Yii know their location).
推荐将资源文件放到Web目录以避免不必要的发布资源过程,这就是之前的例子指定 yii\web\AssetBundle::basePath 而不是 yii\web\AssetBundle::sourcePath.
对于 扩展来说,由于它们的资源和源代码都在不能Web访问的目录下, 在定义资源包类时必须指定yii\web\AssetBundle::sourcePath属性。
注意: yii\web\AssetBundle::sourcePath 属性不要用@webroot/assets,该路径默认为 yii\web\AssetManager资源管理器将源资源发布后存储资源的路径,该路径的所有内容会认为是临时文件, 可能会被删除。
资源依赖
当Web页面包含多个CSS或JavaScript文件时,它们有一定的先后顺序以避免属性覆盖, 例如,Web页面在使用jQuery UI小部件前必须确保jQuery JavaScript文件已经被包含了, 我们称这种资源先后次序称为资源依赖。
资源依赖主要通过yii\web\AssetBundle::depends 属性来指定, 在AppAsset 示例中,资源包依赖其他两个资源包: yii\web\YiiAsset 和 yii\bootstrap\BootstrapAsset 也就是该资源包的CSS和JavaScript文件要在这两个依赖包的文件包含 之后 才包含。
资源依赖关系是可传递,也就是人说A依赖B,B依赖C,那么A也依赖C。
资源选项
可指定yii\web\AssetBundle::cssOptions 和 yii\web\AssetBundle::jsOptions 属性来自定义页面包含CSS和JavaScript文件的方式, 这些属性值会分别传递给 yii\web\View::registerCssFile() 和 yii\web\View::registerJsFile() 方法, 在视图 调用这些方法包含CSS和JavaScript文件时。
注意: 在资源包类中设置的选项会应用到该包中 每个 CSS/JavaScript 文件,如果想对每个文件使用不同的选项, 应创建不同的资源包并在每个包中使用一个选项集。
例如,只想IE9或更高的浏览器包含一个CSS文件,可以使用如下选项:
public $cssOptions = ['condition' => 'lte IE9'];
这会是包中的CSS文件使用以下HTML标签包含进来:
<!--[if lte IE9]> <link rel="stylesheet" href="path/to/foo.css"> <![endif]-->
为链接标签包含
public $cssOptions = ['noscript' => true];
为使JavaScript文件包含在页面head区域(JavaScript文件默认包含在body的结束处)使用以下选项:
public $jsOptions = ['position' => \yii\web\View::POS_HEAD];
Bower 和 NPM 资源
大多数 JavaScript/CSS 包通过Bower 和/或 NPM管理, 如果你的应用或扩展使用这些包,推荐你遵循以下步骤来管理库中的资源:
修改应用或扩展的 composer.json 文件将包列入require 中, 应使用bower-asset/PackageName (Bower包) 或 npm-asset/PackageName (NPM包)来对应库。
创建一个资源包类并将你的应用或扩展要使用的JavaScript/CSS 文件列入到类中, 应设置 yii\web\AssetBundle::sourcePath 属性为@bower/PackageName 或 @npm/PackageName, 因为根据别名Composer会安装Bower或NPM包到对应的目录下。
注意: 一些包会将它们分布式文件放到一个子目录中,对于这种情况,应指定子目录作为 yii\web\AssetBundle::sourcePath属性值,例如,yii\web\JqueryAsset使用 @bower/jquery/dist 而不是 @bower/jquery。
使用资源包
为使用资源包,在视图中调用yii\web\AssetBundle::register()方法先注册资源, 例如,在视图模板可使用如下代码注册资源包:
use app\assets\AppAsset; AppAsset::register($this); // $this 代表视图对象
如果在其他地方注册资源包,应提供视图对象,如在 小部件 类中注册资源包, 可以通过 $this->view 获取视图对象。
当在视图中注册一个资源包时,在背后Yii会注册它所依赖的资源包,如果资源包是放在Web不可访问的目录下,会被发布到可访问的目录, 后续当视图渲染页面时,会生成这些注册包包含的CSS和JavaScript文件对应的 和

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

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

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How to achieve the playback of pictures like videos? Many times, we need to implement similar video player functions, but the playback content is a sequence of images. direct...

In React projects, we often encounter problems with the use of lifecycle functions, especially when it comes to page refresh, how to ensure that certain operations only...

Regarding the problem of inconsistent width of emsp spaces in HTML and Chinese characters in many web tutorials, it is mentioned that occupying the width of a Chinese character, but the actual situation is not...

How to achieve upward scrolling loading similar to WeChat chat records? When developing applications similar to WeChat chat records, a common question is how to...

How to realize the function of playing pictures like videos? Many times, we need to achieve similar video playback effects in the application, but the playback content is not...

Using react-transition-group in React to achieve confusion about closely following transition animations. In React projects, many developers will choose to use react-transition-group library to...

1.0.1 Preface This project (including code and comments) was recorded during my self-taught Rust. There may be inaccurate or unclear statements, please apologize. If you benefit from it, it's even better. 1.0.2 Why is RustRust reliable and efficient? Rust can replace C and C, with similar performance but higher security, and does not require frequent recompilation to check for errors like C and C. The main advantages include: memory security (preventing null pointers from dereferences, dangling pointers, and data contention). Thread-safe (make sure multi-threaded code is safe before execution). Avoid undefined behavior (e.g., array out of bounds, uninitialized variables, or access to freed memory). Rust provides modern language features such as generics
