How do I access Layout object in Zend Framework plugin?

4.1k Views Asked by At

I am trying to use a layout for each actions in a controller. For example, I have three actions in index controller. Those are indexAction, testAction, and welcomeAction. I created three xml layout files. index.xml, test.xml, and welcome.xml. Finally, I created a plugin.

class Moon_Layout_Append extends Zend_Controller_Plugin_Abstract{

 public function preDispatch($request){


  $layoutFile = APPLICATION_PATH."/Modules/".$request->module."/layout/".$request->action.".xml";

  $layout = new Zend_Config_Xml($layoutFile,'index');


 }

}

the problem is...how do I access layout object to set content that I read from xml?

3

There are 3 best solutions below

3
On BEST ANSWER

Layout functionality is easily accessed via a Controller Action helper. You should be able to retrieve the layout helper using the following line.

$layout= Zend_Controller_Action_HelperBroker::getStaticHelper('Layout');

If you're not using the Zend_Application or another way of initialising the Layout, you may need to call the following first:

Zend_Layout::startMvc();
0
On

For me worked only

$layout = Zend_Layout::getMvcInstance(); 

and

 $layout= Zend_Controller_Action_HelperBroker::getStaticHelper('Layout');

didn't work.

0
On

This tutorial tell exactly what you need, you can create a xml for navigation and display this in your layout.phtml

http://www.goodcomputingtips.com/site/2010/09/part-5-adding-menus-using-zend_navigation-a-not-so-quick-quickstart-to-zend-framework/

A quick example:

In your bootstrap

protected function _initNavigation() {
$this->bootstrap("layout");
$layout = $this->getResource('layout');     
$view = $layout->getView();

$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml','nav');
$navigation = new Zend_Navigation($config);

$view->navigation($navigation); }

In your layout.phtml

<?php echo $this->navigation()->menu()->setMaxDepth(1); ?>

In the link you have other ways to configure.