开发者

Zend Framework Render different view within module/controller

I'm using various modules and a parent contro开发者_Python百科ller 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.


I fixed it by using:

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


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


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


You can try (Zend Doc):

 $View = new Zend_View();
 $View->addScriptPath('/path/to/script');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜