Yii2 provides a standard solution to share data between views and layout, through the params
property of the View component that you can use to share data among views.
Note
This is a standard solution since the params
property exists in all views and it is attached to the View component.
This property, params
, is an array that we can use without any restriction.
Imagine that we want to fill the breadcrumb element in the layout to track the path of navigation.
Open the main layout at views/layouts/main.php
; you should find the default implementation of breadcrumb just before declaring the footer:
<div class="container"> <?= Breadcrumbs::widget([ 'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [], ]) ?> </div>
We need to fill the breadcrumbs property of params
in view to display from any view to the layout custom path. For example, we want to display...