php 在 Yii2 中如何管理资产?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25686904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:57:05  来源:igfitidea点击:

How do I manage assets in Yii2?

phpassetsyii2

提问by paus

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. Where should I declare all this stuff in 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:

例如,我创建了一个新页面,我想使用,例如,backbone.js、自定义 css 文件和一些图像集合。我应该在哪里声明 Yii2 中的所有这些东西?我找到了 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?

我究竟做错了什么?

回答by Ivo Renkema

It took me a while to figure it out, but below is the relevant partof 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 $sourcePathis set, and $basePathand $baseUrlare not set(!). The latter tripped me up, and it looks like the same goes for you.

所以Yii2将公布资产只有$sourcePath被设定,$basePath并且$baseUrl未设置(!)。后者把我绊倒了,看起来你也一样。

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);

回答by Herokiller

to use this AppAsset or any other you should register it in the view

要使用这个 AppAsset 或任何其他你应该在视图中注册它

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

回答by Correcter

At first, you should create SomeAsset class in your app/assets/ folder with your new js and css files. You can extend your AppAsset overloading its properties.

首先,您应该使用新的 js 和 css 文件在 app/assets/ 文件夹中创建 SomeAsset 类。您可以扩展您的 AppAsset 重载其属性。

Next use this in SomeController

接下来在 SomeController 中使用它

use Yii;
use app\assets\SomeAsset;

and in actionSome like this:

在行动中有些像这样:

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

回答by Keith Grey

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');