The resources in Yii are files related to the Web page, which can be CSS files, JavaScript Files, pictures or videos, etc., 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 divided according to their location into:
Source resources: Resource files and PHP source code are put together and 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 will be introduced in detail later.
Publish resources: Resource files are placed in a Web directory that can be accessed directly 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.
对于 扩展来说,由于它们的资源和源代码都在不能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文件对应的 和