Passing Data from Controller to Layout in Yii 2
In Yii 1, passing data to a layout was straightforward because the view context and the application context shared a loose coupling that made global variables feel accessible. Developers could simply assign a property on the controller or access $this->param directly within the layout file, assuming main.php lived inside the layouts directory and inherited the controller's state.
However, Yii 2 introduced a more rigid separation of concerns. The framework no longer automatically exposes controller properties to layouts in the way you might expect from legacy versions. When you call $this->render('index', ['param' => $paramValue]), you are passing data specifically into the views/index view file. The layout, typically located at layouts/main.php, remains distinct. Accessing that parameter directly inside the layout using $this->param or a bare $param will fail because the layout does not automatically inherit the view's context variables passed during rendering unless you explicitly bridge the gap.
To retrieve a value like $paramValue in your main.php layout, you must understand how Yii 2 handles the view rendering chain. The layout is rendered first, then the body content is inserted. At the time the layout is being processed, the variables passed to the specific view (like index) are not automatically injected into the layout's scope.
There are two primary patterns to solve this, depending on your architectural needs.
Storing Data in Application Scope
The most robust solution for data that must exist across multiple views and layouts is to store the value in the application or controller scope before rendering begins.
If you need this parameter available globally within the layout, assign it to the $app instance or a specific model:
// In your Controller action
$app = \Yii::$app;
$app->paramValue = $paramValue; // Or use a custom section in session if needed
Inside your main.php layout, you can access this via \Yii::$app->paramValue. While using the application component directly is often discouraged for simple state management due to memory implications in long-running processes, it is perfectly valid for passing temporary flags or configuration data that needs to bleed into the header/footer without modifying every view.
Passing Data Explicitly via View Context
If your intention is to pass data specifically for the current page's layout section, you must ensure the variable exists in the view context before the layout renders the body. You cannot rely on the view variables being "leaked" into the layout automatically.
A clean approach is to define a dedicated property in the controller and assign it before rendering:
public function actionIndex()
{
$paramValue = 'My Dynamic Content';
// Assign to the view context explicitly
$this->view->params['param'] = $paramValue;
return $this->render('index');
}
In your main.php layout, you can now access $this->params['param']. This leverages Yii 2's standard parameter array which is accessible in both the layout and the view. It avoids creating global app properties for transient data.
Why Direct Rendering Fails
The confusion often stems from trying to pass data via the second argument of render() expecting it to appear globally in the layout. The render() method takes a view file name and an array of variables intended strictly for that view's scope (views/index.php). When the renderer executes, it calls the layout with $view->params merged into the context, but only if you explicitly populate $this->view->params.
If you are using a custom layout class or extending yii\web\Layout, remember that the layout instance is created fresh for each request. It does not hold state from previous views unless you persist it in the session or database.
Alternative: Using Sections
If your goal is to keep the main layout clean but inject specific data into certain parts of it, Yii 2 supports rendering partials within the layout itself. You can pass the parameter to a partial view and include that only where necessary, rather than cluttering the entire layout's context with variables you only need in one spot.
// In main.php layout
<?= $this->render('_header', ['param' => $paramValue]); ?>
This pattern keeps your global layout logic decoupled from specific view requirements, adhering to the separation of concerns that Yii 2 enforces.