views:

187

answers:

2

I seem to have a problem with my layout displaying twice. I was wondering if this was caused by something in my front controller plugin (see below) which utilises Zend Layout only for a particular module and not in the main application. Any answers will be much appreciated. Thanks

class My_Controller_Plugin_AdoptLayout extends Zend_Controller_Plugin_Abstract

{

 public function preDispatch(Zend_Controller_Request_Abstract $request)
 {

   $moduleName = $request->getModuleName();    

   if ($moduleName == 'some-module') {

     if (!Zend_Layout::isEnabled()) {

       $view = new Zend_View(array('encoding' => 'UTF-8'));
       $view->addScriptPath(ROOT_DIR . '/modules/some-module/views/scripts/');
         $view->doctype('XHTML1_STRICT');

       $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
         Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);  

       Zend_Layout::startMvc();
       $layout = Zend_Layout::getMvcInstance();
       $layout->setLayout('default');
       $layout->setLayoutPath(APPLICATION_PATH . '/modules/some-module/views/scripts/layouts/');
       $layout->setView($view);      
     }

   }               
 }

}

+1  A: 

I have run into this problem when using _forward or when I redirected to the errorController within a preDispatch plugin. You could check for these things.

Also i'm a little suspicious about the line:

$layout->setView($view);

Is it necessary. Doesn't Zend automatically assign the view to the layout.

andho
Thanks andho. I wondered if it may be _forward actually but I really need to use it as I have to pass form values between actions and can't seem to get it to work with $this->_helper->redirector.I had to create a new view object because the main application uses a Zend_View/Smarty hybrid and I don't want to change all my templates as they are pure Zend_View.
sagittariidae
Why would you need to pass form data to another action? Is the action in the same Controller. If so can't you call $this->otherAction(); Or use action chaining but this might potentially render another view unless you switch off rendering for either one of the actions
andho
+1  A: 

This is typically caused by the ErrorController in my experience. I usually clear the response to make sure there's no nesting:

public function errorAction() {

        $this->getResponse()->clearBody();

//handle error ........

}
David Caunt