Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework Render different view within module/controller

I'm using various modules and a parent controller to keep consistancy.

My parent controller has a bunch of common actions that each of the subsequent child controllers need to have access to. I'm finding it difficult to make this default action render a correct view.

I do NOT want to have to add the same view to each of my modules - that would defeat the object - but i would like to have a default view somewhere.

I have tried:

$this->_forward('commonaction', 'baselayout', 'default');

This doesnt work for me - as it tries to process the action again - when i've already populated the variables needed within my parent controller.

Any help would be awesom.

UPDATE:

To clarify i want to be able to use a common view from a different module. All exampels and solutions currently assume a common module. THis doesnt work for me.

like image 651
Andy Avatar asked Oct 06 '11 11:10

Andy


4 Answers

I fixed it by using:

$this->_helper->viewRenderer->renderBySpec('foo', array('module' => 'modulename', 'controller' => 'controllername')); 
like image 197
fr0sty Avatar answered Feb 01 '23 06:02

fr0sty


Use this statement in your controller:

// Render 'foo' instead of current action script
$this->_helper->viewRenderer('foo');

// render form.phtml to the 'html' response segment, without using a
// controller view script subdirectory:
$this->_helper->viewRenderer('form', 'html', true);

from the official doc http://framework.zend.com/manual/en/zend.controller.actionhelpers.html

It is not possible (at the best of my knowledge) to show view of different module. Besides there's no trace of this question in the official doc.

like image 36
Aurelio De Rosa Avatar answered Feb 01 '23 06:02

Aurelio De Rosa


Like you, I have multiple modules and wanted to render an arbitrary view (residing in one module) in the controller of a different module.

This was the only way that I could get it to work, after trying many approaches:

Controller action:

public function xyzAction()
{
    $this->_helper->layout->disableLayout();
    $view = new Zend_View();
    $view->setScriptPath(APPLICATION_PATH . '/modules/moduleB/views/scripts/abcBlahBlah');
    echo $view->render('yadaYada.phtml');
    exit;
}
like image 33
Ryan Avatar answered Feb 01 '23 06:02

Ryan


Try the following in your action:

  • First you specify the path to the module where your view script is stored
  • Then you specify the name of the view script to the view renderer

See the following code:

// Add a script path to the directory in the other module
$this->view->addScriptPath(APPLICATION_PATH . '/modules/baselayout/views/scripts');
// Tell the viewRenderer to use the view script 'scriptname'
$this->_helper->viewRenderer('scriptname');
like image 44
halfpastfour.am Avatar answered Feb 01 '23 06:02

halfpastfour.am