yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.9k forks source link

$baseUrl is not automatically setting in Asset Bundle if using grouping #14686

Open hello-omny opened 7 years ago

hello-omny commented 7 years ago

What steps will reproduce the problem?

Create asset bundle, resource files must be stored not in @webroot, for example something like this @app/themes/night/assets.

In my case i have:

img/
  logo.png
css/
  style.css
js/
  script.js

My asset bundle:

namespace app\themes\night;

use yii\web\AssetBundle;

class MainPageAsset extends AssetBundle
{
    public $sourcePath = '@app/themes/night/assets';

    public $css = [ 'css/style.css' ];

    public $js = [ 'js/script.js' ];

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

In view i get base url of image:

$bundle = MainPageAsset::register($this);
$assetsPrefix = $bundle->baseUrl;

On this step $baseUrl = /assets/2a669ca1 it Isn't null, that's ok. I use it for build path to img:

<img src="<?= $assetsPrefix ?>/img/logo.png">

I use guide for grouping asset bundles, have been build that i needed, all worked perfect, but!

What is the expected result?

$bundle = MainPageAsset::register($this);
$assetsPrefix = $bundle->baseUrl;

I expecting $assetPrefix = /assets/2a669ca1.

What do you get instead?

I have got $assetPrefix = null.

Additional info

Images, styles and scripts of my bundle and other have been created normally at @web/assets/....

Q A
Yii version 2.0.11.2
PHP version 7.0.22
Operating system macOS 10.12.5
cebe commented 7 years ago

if you use assets for grouping JS/CSS files as well as publishing other content, you should probably separate the other content from JS/CSS into a separate AssetBundle to have them published even if CSS/JS goes through the minified way. The MainPageAsset does not get published as normal with the asset grouping approach so files that are not JS or CSS are not available.

cebe commented 7 years ago

Not sure it is possible to achieve what you are asking for. Its better to separate assets imo.

hello-omny commented 7 years ago

Thx for reply!

I can't separate images from css, because i need link it in CSS like this:

.play-icon {
    background-img: url (../img/play.gif);
}

This code work fine for me.

The MainPageAsset does not get published as normal with the asset grouping approach so files that are not JS or CSS are not available.

Bundle is published ok. I had tried it just now, asset folder have been cleared before it. Images and other files have been copied to right place, scripts and styles is there too. But path and url to there location does not stored.

lesha724 commented 6 years ago

Hi @hello-omny, you can use something like this:

$directoryAsset = Yii::$app->assetManager->getPublishedUrl('@vendor/almasaeed2010/adminlte/dist');

or [AssetManager::getAssetUrl()](https://www.yiiframework.com/doc/api/2.0/yii-web-assetmanager#getAssetUrl()-detail)