Sharing data between views and layout

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

Get Yii2 By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.