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
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.
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’]