Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

php - How to pass param from controller to layout in YII2

I want to send a parameter from controller to layout (i.e. main.php). But I am not able to get the param in main.php

I tried:

Controller Code:

$this->render('index',array('param' => $paramValue));

And this is how i was trying to get this in layout ie. main.php

  1. $this->param (as in yii 1)
  2. $param

But i am not able to get param value in layout. Can anyone tell me how to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

yiiaseView has special $params property.

For example it's used for building breadcrumbs in default generated CRUD code templates with Gii.

You can set it like this before rendering:

use Yii;

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

Inside a controller you can set it like this:

$this->view->params['customParam'] = 'customValue';

Then it will be available in views (including main layout):

/* @var $this yiiwebView */

echo $this->params['customParam'];

You can also find it in official guide.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...