Search code examples
zend-framework2

access controller action variables to zf2 layout


I have created two layouts: one for the login page and another for the rest of the site. After login I want to pass variables from login controller to zf2 layout, but not able to access.

How can i access the variables in layout created in a controller action?


Solution

  • Some ways to do that in your controller:

    Using view model

    $viewmodel = new ViewModel();
    $viewmodel->setVariable('myvar', $myvar);
    return $viewmodel;
    

    Using layout()

    $this->layout()->myvar = $myvar;
    

    Into your view script

    <?php echo $this->myvar; ?>