Yii Asset manager - css with image url

↘锁芯ラ 提交于 2019-12-07 03:46:31

问题


I am using the following code to publish my css file to assets.

Yii::app()->clientScript->registerCssFile( Yii::app()->assetManager->publish( Yii::getPathOfAlias('application').'/../css/combo-autocomplete.css' ));

It registers the css successfully and styles are working fine, but the following css class using a image.

.ui-icon {
   background-image: url(../images/down-arrow.png); 
}

is not loading, it is referred to path inside assets folder. It is looking for the image inside

root/assets/images/

Is there any way to redirect the image url in the css to the original path, because i am not registering my images as assets as of now.

root/images


回答1:


Yii's asset manager is designed to let you package components such as UI widgets in a self-contained manner (the alternative being to manually distribute various files in different places inside your application directory structure). So if it makes sense to publish anything as an asset from your component, it makes sense to publish everything as an asset.

In that case, you should structure your component e.g. as

component/
    assets/
        css/
        images/
        js/

Then, publish the whole assets/ directory using Yii's asset manager instead of publishing files one by one. If you do this, they will end up being published in a manner like

assets/
    random_hash/
        css/
        images/

Your CSS can then simply refer to images with url(../images/image.jpg) without needing to know the value of random_hash.



来源:https://stackoverflow.com/questions/12954203/yii-asset-manager-css-with-image-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!