Layout params to view

So, in my "layouts/main.php" i have something like this




<?php

use frontend\assets\AppAsset;

$bundle = AppAsset::register($this);

.....

echo $content ;


?>



And in views/site/index.php i have:


<div id="index">


 <?= Html::img($bundle->baseUrl . '/img/a2.jpg', ['alt'=>'some', 'class'=>'img-circle m-t-xs img-responsive']);?> 

....

</div>

But Yii throws an Notice Error saying that $bundle is not defined, what i have to do is to rewrite this in views/site/index.php:




use frontend\assets\AppAsset;

$bundle = AppAsset::register($this);



I want to ask if this is the right way to do so, will this re-declaration affect the performance. If it is, is there any way to access the layout params from the view file (In this case, ‘views/site/index.php’.

Thanks very much

I have not tried if you can use $view object inside the layout:




Yii::$app->view->params['bundle'] = $bundle;



and then reuse it inside the view.

http://www.yiiframework.com/doc-2.0/guide-structure-views.html#sharing-data-among-views

Thanks for your super fast reply, I will try your solution now. But what is the best way of yours to access layout params in view file? Cos i want to access the baseUrl of my Theme.

Thanks Fabrizio Caldarelli

Usually I don’t access to layout params from the view.

Other this case with $bundle variable, why should you access to layout params from the view?

Sorry for being late to reply, i can only post 2 replies per 12 hours.

Anyway problem solved as i want to access the baseUrl of my theme i can do this:


Yii::$app->view->theme->baseUrl

Thanks Fabrizio Caldarelli