Yii2 AssetManager in Layout

I have created my own bootstrap AssetManager and I want to include it in the layout so I don’t have to define it on every single page.

Like so but since the layout is loaded after I don’t get access to the variables.

Layout file




<?php

/** @var $this  yii\web\View */

/** @var string $content */


use app\assets\MyAsset;

$my_asset = MyAsset::register($this);

?>

<?php $this->beginPage();?>

<!DOCTYPE html>

<html lang="en">

<head>

    <title><?= $this->title;?></title>

    <?php $this->head();?>

</head>

<body>

<?php $this->beginBody();?>

<?= $content;?>

<?php $this->endBody();?>

</body>

</html>

<?php $this->endPage();?>



View File




<div class="logo">

    Html::img($my_asset->baseUrl . '/img/logo.png');

</div>



If I have to I can add it to each view. It just seems like a lot of replicated code and if I have to change, it will be a pain.

Let me know if I am going about this wrong of if there is another way.

Hey, @scottix.

Just use




Html::img('/img/logo.png');



That is the problem, the image is in the asset folder. It will show blank.

Web is the only directory which is public. Images must be there.

No you are wrong assets publishes a private folder to the public folder as public/assets/12345/img/logo.png.

I am sorry you don’t know what you are talking about.

And assets not under web directory? )

Please stop wasting people’s intellect, you have no way contributed any help to my problem and go read http://www.yiiframework.com/doc-2.0/guide-structure-assets.html

Nothing against you, but you have no idea what I am talking about.

Stop wasting our time Scottix.

Locked for having crossed the line of what is tolerated behaviour.

[color="#006400"]/* unlocked */[/color]

Edit:

Umneeq is a valuable member of our community and despite his low post count he has already gone out of his way to help out.

Good advice would be to try and put yourself in other peoples shoes and also take different culture into consideration.

Hi Umneeq,

Sorry if I have offended your answers. I felt you did not understand my problem and I did not respond appropriately. I hope we can get back to talking about Yii2 :)

Best,

Scott

Thank you guys for your undertanding. My bad I don`t understand the question appropriately. @scottix, maybe you was asking about https://github.com/yiisoft/yii2/blob/master/framework/web/AssetBundle.php#L43 ?

And if you don’t set it you code will be like this:




Html::img($my_asset->basePath . '/img/logo.png');



I did some diggin and I think I found a solution I can live with. The problem is the view gets rendered before the layout gets rendered. So the view will never have those assets in time. So we need to do it before render which is in the controller.

I created a Filter to register the custom bootstrap asset.




namespace app\components;


use yii\base\ActionFilter;


use app\assets\MyAsset;


class MyFilter extends ActionFilter

{

    public $_view;


    public function beforeAction($action)

    {

        $my_asset = MyAsset::register($this->_view);

        Yii::setAlias('@my', $my_asset->baseUrl);

        return parent::beforeAction($action);

    }

}



In the controller I defined a behavior as the following.




    public function behaviors()

    {

        return [

            [

                'class' => 'app\components\MyFilter',

                '_view' => $this->getView(),

            ]

        ];

    }



Now any view with that controller I can have. Without having to register it in every view. Actually I can control it where this happens as well. Which is a plus.




<?= Html::img('@my/img/logo.png');?>



or you could override the view class, and use the beforeRender event to do what you want. don’t forget to call the parent beforeRender as well.




namespace frontend\components;


class myView extends yii\base\View

{

public function beforeRender

{

     //...your asset publication here


    parent::beforeRender();

}



Then in your configs set ‘view’ => [‘class’ => ‘frontend\components\myView’]