54

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?

gvlasov
  • 18,638
  • 21
  • 74
  • 110
Amit Kumar
  • 3,384
  • 6
  • 25
  • 42
  • http://www.yiiframework.com/doc-2.0/guide-structure-views.html#creating-layouts you shoulld reread manual and look at `->beginContent()` method. – user1954544 Jan 20 '15 at 06:50
  • You can use $this->context to do this. See my answer: https://stackoverflow.com/a/60463356/884177 – smohadjer Feb 29 '20 at 08:42

6 Answers6

98

yii\base\View 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 yii\web\View */

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

You can also find it in official guide.

arogachev
  • 33,150
  • 7
  • 114
  • 117
3

I would like to suggest you some steps for this problem.

  1. pass parameter to view file
  2. Set parameter to view parameter
  3. Check For parameter and If exist then use it.

    //in controller method
    $this->render("view-file-name",["paramName" => "some parameter"]);
    
    //in view file for eg: index.php
    //i'm passing paremeter sent form controller's action to view params.
    $this->params["paramFromViewFile"] = $paramName; //here $paramName is the parameter we sent from controller's method
    
    //access parameter sent from view file
    if($this->params && !empty($this->params["paramFromViewFile"]))
    {
         echo $this->params["paramFromViewFile"];  
    }
    
msucil
  • 806
  • 1
  • 8
  • 15
2

In Yii2 you can access your controller object from a view using $this->context for example if in your controller you set a parameter like this:

public $title = 'My custom title';

Then in your layout you can read it like this:

$this->context->title

It is explained in Yii documentation in section: Accessing Data in Views

smohadjer
  • 801
  • 1
  • 9
  • 24
0

Thats because you are rendering index.php view, not the main.php one.

And $param this is how you get it in 1.1 version.

UPD: If you want a param in your main.php layout declare it in your Controller class, and then you will be able to get it $this->param this way.

UPD2: Looks like in 2.0 version you need to declare param in a yii\web\View class. And access it via Yii::$app->view->param.

Vick
  • 287
  • 2
  • 8
0

directly calling $param u should get the value, try

in controller declare a $param,
public $param = '';

then in the acction do
$param='haha';

in layout
echo $this->param;
onegun
  • 803
  • 1
  • 10
  • 27
0

I had a same problem, I resolve that in:

  1. In my Controller par exemple, I do that

    $this->view->params['typeUser'] = 'Internaut';

  2. in my View

    $this->params['typeUser'] or echo $this->params['typeUser']

user206
  • 1,085
  • 1
  • 10
  • 15