ZEND Controllers -- How to call an action from a different controller

15k Views Asked by At

I want to display a page that has 2 forms. The top form is unique to this page, but the bottom form can already be rendered from a different controller. I'm using the following code to call the action of the other form but keep getting this error:

 "Message: id is not specified"

 #0 .../library/Zend/Controller/Router/Rewrite.php(441): Zend_Controller_Router_Route->assemble(Array, true, true)

My code:

First controller:

abc_Controller
public function someAction()
{

    $this->_helper->actionStack('other','xyz');

}

Second controller:

    xyz_Controller
 public function otherAction()
 {
 // code
 }

Desired results:

When calling /abc/some, i want to render the "some" content along with the xyz/other content. I think I followed the doc correctly (http://framework.zend.com/manual/en/zend.controller.actionhelpers.html) but can't find any help on why that error occurs. When I trace the code (using XDebug), the xyz/other action completes ok but when the abc/some action reaches the end, the error is thrown somewhere during the dispatch or the routing.

Any help is greatly appreciated.

8

There are 8 best solutions below

0
On

Just an update. The error had absolutely nothing to do with how the action was being called from the second controller. It turns out that in the layout of the second controller, there was a separate phtml call that was throwing the error (layout/abc.phtml):

<?php echo $this->render('userNavigation.phtml') ?>

line of error:

echo $this->navigation()->menu()->renderMenu(...)

I'll be debugging this separately as not to muddy this thread.

Thanks to Akeem and hsz for the prompt response. I learned from your responses.

To summarize, there were 3 different ways to call an action from an external controller:

  1. Instantiate the second controller from the first controller and call the action.
  2. Use $this->_helper->actionStack
  3. In the phtml of the first controller, action('other','xyz');?> (as Akeem pointed out above)

Hope this helps other Zend noobs out there.

0
On

The urge to do something like this is an indication you're going about it in totally the wrong way. If you have the urge to re-use content, it should likely belong in the model. If it is truly controller code it should be encapsulated by an action controller plugin

4
On

You can create new object with second controller and call its method (but it`s not the best way).

You can extend your first controller with the second one and call $this->methodFromSecond(); - it will render second form too with its template.

BTW - what type of code you want to execute in both controllers ?

1
On

You can accomplish this in your phtml for your someAction. So in some.phtml put <?php echo $this->action('other','xyz');?> this will render the form found in the otherAction of XyzController

0
On

In phtml file u can use the $this->action() ; to render the page and that response would be added to current response ..

The syntax for action is as follows::

public function action($action, $controller, $module = null, array $params = array())
0
On

This kind of route definition:

routes.abc.route = "abc/buy/:id/*" 
routes.abc.defaults.controller = "deal" 
routes.abc.defaults.action = "buy" 
routes.abc.reqs.id = "\d+"

requires a parameter in order to function. You can do this with actionStack but you can also specify a default id in case that none is provided:

$this->_helper->actionStack('Action',
                            'Controller',
                            'Route',
                            array('param' => 'value')
);


routes.abc.defaults.id = "1" 
0
On

For Me this worked like a charm

    class abcController extends Zend_Controller_Action
    {
        public function dashBoardAction()
        {
            $this->_helper->actionStack('list-User-Data', 'xyz');
        }
    }

    class XyzController extends Zend_Controller_Action {
        public function listUserDataAction()
        {
            $data = array('red','green','blue','yellow');
            return $data;
        }
    }
0
On

Hm I can't find and idea why you need to use diffrent Controlers for one view. Better practise is to have all in one Controller. I using this like in this example

DemoController extends My_Controller_Action() {
 ....
 public function indexAction() {
   $this->view->oForm = new Form_Registration();
 }
}

My_Controller_Action extends Zend_Controller_Action() {
   public function init() {
      parent::init();
      $this->setGeneralStuf();
   }

   public function setGeneralStuf() {
       $this->view->oLoginForm = new Form_Login();
   }
}