简体   繁体   English

如何在Yii2中管理资产?

[英]How do I manage assets in Yii2?

For example, I created a new page, and I'd like to use, for example, backbone.js, custom css file and some collection of images. 例如,我创建了一个新页面,我想使用例如backbone.js,自定义css文件和一些图像集合。 Where should I declare all this stuff in Yii2? 我应该在哪里宣布Yii2中的所有这些东西? I found the AppAsset.php module, but this is only for css/js files and I haven't noticed any changes when my css/js files and path were declared there: 我找到了AppAsset.php模块,但这只适用于css / js文件,当我在那里声明我的css / js文件和路径时,我没有注意到任何变化:

class AppAsset extends AssetBundle {
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
        'js/jquery.mobile-1.4.2.min.css',
    ];
    public $js = [
        'js/jsquery-2.1.0.min.js',
        'js/jquery.mobile-1.4.2.min.js',
        'js/script.js',
    ];

    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}

What am I doing wrong? 我究竟做错了什么?

It took me a while to figure it out, but below is the relevant part of the Yii2 source code 我花了一段时间来弄明白,但下面是Yii2源代码的相关部分

if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
    list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
}

So Yii2 will publish assets only if $sourcePath is set, and $basePath and $baseUrl are not set(!). 因此,只有在设置了$sourcePath并且未设置$basePath$baseUrl baseUrl时,Yii2才会发布资产(!)。 The latter tripped me up, and it looks like the same goes for you. 后者绊倒了我,看起来你也一样。

So I have this AppAsset, which duly publishes 所以我有这个适当发布的AppAsset

use yii\web\AssetBundle;


class AppAsset extends AssetBundle
{
public $sourcePath = '@app/assets/app';

public $css = [
    'css/openbook.css',
    'fontello/css/fontello.css',
    'fontello/css/animation.css'
];
public $js = [
    'js/plug.openbook.js',
    'js/plug.interpret.js',
    'js/plug.drop.message.js'
];
public $depends = [
   // 'yii\web\YiiAsset', 
   // 'yii\bootstrap\BootstrapAsset',
];
} 

Of course, I have in the main layout 当然,我在主要布局中

use frontend\assets\AppAsset;
...
AppAsset::register($this);

to use this AppAsset or any other you should register it in the view 要使用此AppAsset或任何其他,您应该在视图中注册它

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

At first, you should create SomeAsset class in your app/assets/ folder with your new js and css files. 首先,您应该使用新的js和css文件在app / assets /文件夹中创建SomeAsset类。 You can extend your AppAsset overloading its properties. 您可以扩展AppAsset重载其属性。

Next use this in SomeController 接下来在SomeController中使用它

use Yii;
use app\assets\SomeAsset;

and in actionSome like this: 并在行动中像这样:

SomeAsset::register(Yii::$app->view);

From personal experience, assets are one of the parts of Yii that I found extremely frustrating. 从个人经验来看,资产是Yii的一个部分,我觉得非常令人沮丧。

It is hard to reliably find out where the file is going to be and the switching back and forth with debug mode on and off will create further frustration. 很难可靠地找出文件的位置,并且打开和关闭调试模式来回切换会造成进一步的挫败感。

I suggest scrapping the asset handling and just keep all your JS files in a folder, then it can be included like this: 我建议废弃资产处理并将所有JS文件保存在一个文件夹中,然后它可以包含如下:

Yii::app()->clientScript->registerScriptFile('/js/jquery.jeditable.mini.js');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM