Sending variables to the layout in Zend Framework

2020-01-25 05:57发布

In my project I have a number of dynamic elements that are consistently on every page. I have put these in my layout.phtml

My question is: How can I send variables into my layout from my controllers?

If I want to send things from my controller I can use:

$this->view->whatever = "foo";

And receive it in the view with

echo $this->whatever;

I cannot figure out how to do the same with my layout. Perhaps there is a better way around the problem?

8条回答
疯言疯语
2楼-- · 2020-01-25 06:07

The standard view variables are available if you use the layout within the MVC. In bootstrap file, include this:

Zend_Layout::startMvc();

You must then tell each controller (or even each action, if you wanted granular control over several different layouts) which layout to use. I put mine in the init() of each controller. Here's an example, if your layout file is named layout.phtml:

$this->_helper->layout->setLayout('layout');
查看更多
萌系小妹纸
3楼-- · 2020-01-25 06:12

Without using helpers or plugins do :

Zend_Layout::getMvcInstance()->assign('whatever', 'foo');

After this you can use the following in your layout:

<?php echo $this->layout()->whatever; ?>

This will print "foo".

查看更多
冷血范
4楼-- · 2020-01-25 06:18

View Helpers are also a good idea. I had a ecommerce website, which I had a layout.phtml with menus with categories and subcategories that I needed to bring from the database.

For this, I did the following:

Bootstrap.php:

protected function _initHelperPath() 
{

    $view = $this->bootstrap('view')->getResource('view');

    $view->setHelperPath(APPLICATION_PATH . '/views/helpers', 'View_Helper');

}

application.ini:

resources.view[]=

In views/helpers, I had a file called Menus:

class View_Helper_Menus extends Zend_View_Helper_Abstract {

    public function categories(){

       $categories = new Application_Model_DbTable_Categories();

       return $categories->fetchAll();

    }

    public function subCategories(){

        $subCategories = new Application_Model_DbTable_SubCategories();

        return $subCategories->fetchAll();

    }

}

In layout.phtml, I just had to call the specific helper, and call the methods from it:

$menu = $this->getHelper('Menus');
$categories = $menu->categories();
$subCategories = $menu->subCategories();

Hope it helps someone that needs to bring data from database to render the layout.

查看更多
时光不老,我们不散
5楼-- · 2020-01-25 06:23

As a side note, if you send json at some point in your app be careful that global view variables are not sent with the response.

查看更多
小情绪 Triste *
6楼-- · 2020-01-25 06:27

I have a implemented a Base Controller which all other controllers extend.

So I have a controller...

<?php
class BaseController extends Zend_Controller_Action
{
  public function init()
  {
    $this->view->foo = "bar";
  }
}

and in the layout and/or view

<?= $this->foo ?>
查看更多
We Are One
7楼-- · 2020-01-25 06:27
class IndexController extends Zend_Controller_Action
{

   public function init()
   {
      $this->_layout = $this->_helper->layout->getLayoutInstance();
      $this->_layout->whatever = $this->view->render('test.phtml);
   }
}

In the layout file you can call

<p><?php echo $this->layout()->whatever ?>

If in some actions if you don't want that section then:

public function viewAction()
{
   $this->_layout->whatever = null;
}
查看更多
登录 后发表回答