9

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.

1
  • I've updated my answer. Aurelio was on the right track but setting adding the script path is required for the use of view script outside of the current module. May 7, 2015 at 8:35

5 Answers 5

7

I fixed it by using:

$this->_helper->viewRenderer->renderBySpec('foo', array('module' => 'modulename', 'controller' => 'controllername')); 
0
6

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.

3
  • Hey Aurelio - thanks the major problem with this solution is that it still looks for the view within the same module that i'm currently in. Which is wrong. I need to be able to say i want view X rendered from Module Y.
    – Andy
    Oct 6, 2011 at 11:58
  • I'm sorry. I understood same module but different controller. Oct 6, 2011 at 11:59
  • ah ok my bad. will update my question accordingly. Do you know if it's possible?
    – Andy
    Oct 6, 2011 at 12:03
2

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;
}
2

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');
-1

You can try (Zend Doc):

 $View = new Zend_View();
 $View->addScriptPath('/path/to/script');
1
  • This is really not what OP is asking for. Setting the script path does not render a different view. May 1, 2015 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.