ZF Perform the action and get html in another action

What I want to do with the Zend Framework is to make action Y from action X and get html:

Example:

public xAction(){ $html = some_function_that_render_action('y'); } public yAction(){ $this->view->somedata = 'sometext'; } 

where the y view looks something like this:

 <h1>Y View</h1> <p>Somedata = <?php echo $this->somedata ?></p> 

I am creating an action helper, but I cannot use it from the controller. How can i solve this? Is it possible?

+4
source share
3 answers

Here is one possible way to do what you want.

 public function xAction() { $this->_helper ->viewRenderer ->setRender('y'); // render y.phtml viewscript instead of x.phtml $this->yAction(); // now yAction has been called and zend view will render y.phtml instead of x.phtml } public function yAction() { // action code here that assigns to the view. } 

Instead of using ViewRenderer to set the view of the script, you can also call yAction, as I showed above, but get html by calling $html = $this->view->render('controller/y.phtml');

See also the ActionStack Assistant .

+2
source

Using controller

You can use the View Action Help.
 public function xAction() { $html = $this->view->action( 'y', $this->getRequest()->getControllerName(), null, $this->getRequest()->getParams() ); } public function yAction() { // action code here that assigns to the view. } 

This is not very pretty, but it works well, and you do not need to use $view->setScriptPath($this->view->getScriptPaths());

This helper creates a new Zend_Controller_Request for yAction (), so you can give your own parameters as the 4th argument or use $this->getRequest()->getParams() to propagate the xAction () request parameters.

http://framework.zend.com/manual/1.12/en/zend.view.helpers.html#zend.view.helpers.initial.action

+1
source

Finally, I found this β€œsolution”, this is not what I want to do, but it works, if someone found a real solution, answer here.

 public function xAction(){ $data = $this->_prepareData(); $view = new Zend_View(); $view->somedata = $data; $view->setScriptPath($this->view->getScriptPaths()); $html = $view->render('controller/y.phtml'); } 
0
source

Source: https://habr.com/ru/post/1394234/


All Articles