Resources in Yii are files related to Web pages, which can be CSS files, JavaScript files, images 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 yiijuiDatePicker widget in a page, it will automatically include the required CSS and JavaScript files, instead of requiring you to manually find these files and include them. When you upgrade a widget, it will automatically use the new version of the resource file. In this tutorial, we will detail the powerful resource management functions provided by Yii.
Resource Pack
Yii manages resources in resource packages. Resource packages are simply a collection of resources placed in a directory. When a resource package is registered in a view, the CSS and JavaScript files in the package will be included when rendering the web page.
Define resource package
The resource package is designated as a PHP class that inherits yiiwebAssetBundle. The package name is a 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 with 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 file specified by the AppAsset class is placed in the @webroot directory, and the corresponding URL is @web. The resource package contains a CSS file css/site.css, no JavaScript file, and relies on the other two packages yiiwebYiiAsset and yiibootstrapBootstrapAsset. About More details on the properties of yiiwebAssetBundle are described below:
yiiwebAssetBundle::baseUrl: Specifies the URL corresponding to the yiiwebAssetBundle::basePath directory. Similar to yiiwebAssetBundle::basePath, if you specify the yiiwebAssetBundle::sourcePath attribute, the resource manager will publish these resources and override this attribute. The path alias can be used here.
yiiwebAssetBundle::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:
Resource location
Resources can be classified according to their location:
Source resources: Resource files and PHP source code are placed together and cannot be directly accessed by the Web. In order to use these source resources, they must be copied to a Web-accessible Web directory to become published resources. This process is called publishing. Resources will be introduced in detail later.
Publish resources: Resource files are placed in a Web directory that can be directly accessed through the Web;
External resources: Resource files are placed on a different web server than your web application;
When defining the resource bundle class, if you specify the yiiwebAssetBundle::sourcePath attribute, it means that any resources using relative paths will be used as source resources; if you do not specify this attribute, it means that these resources are published resources (therefore you should specify yiiwebAssetBundle ::basePath and yiiwebAssetBundle::baseUrl let Yii know their location).
It is recommended to place resource files in the web directory to avoid unnecessary resource publishing process. This is why the previous example specifies yiiwebAssetBundle::basePath instead of yiiwebAssetBundle::sourcePath.
For extensions, since their resources and source codes are in directories that cannot be accessed by the Web, the yiiwebAssetBundle::sourcePath attribute must be specified when defining the resource bundle class.
Note: Do not use @webroot/assets for the yiiwebAssetBundle::sourcePath attribute. This path defaults to the path where the yiiwebAssetManager resource manager stores the source resources after publishing them. All contents of this path will be considered temporary files and may be deleted. .
Resource Dependencies
When a web page contains multiple CSS or JavaScript files, they have a certain order to avoid property overwriting. For example, a web page must ensure that the jQuery JavaScript file has been included before using the jQuery UI widget. We call this The sequence of resources is called resource dependency.
Resource dependencies are mainly specified through the yiiwebAssetBundle::depends attribute. In the AppAsset example, the resource bundle depends on two other resource bundles: yiiwebYiiAsset and yiibootstrapBootstrapAsset. That is, the CSS and JavaScript files of the resource bundle must be in the two dependency bundles. Included after the file is included.
Resource dependencies are transitive, that is, if people say A depends on B and B depends on C, then A also depends on C.
Resource Options
可指定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文件对应的 和