views:

500

answers:

6

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.

A: 

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 ?

hsz
actually, creating the second controller from the first controller was my first attempt. It returns exactly the same error.As for what's executing, the first controller renders a simple form -- a single text box (# of items to purchase), the second controller calls the another form (a credit card form used in other parts of the system).
Do you use Zend_Form ? If yes - you can create objects of them in both of controllers and push them to the View. It's clean way.If you want to handle them in the same way (check if valid, update in database, etc) you should make helpers extended with `Zend_Controller_Action_Helper_Abstract` for every of them.In them you can do anything you do in controllers.I hope that I explained it clearly. ;)
hsz
Yes, I use Zend_Form on both controllers. I tried creating the objects explicitly instead of using the _helper->actionStack but results are the same. I think it must have to do with my routes. I'm checking into that now.
I agree with hsz, see my answer
Josh Ribakoff
+1  A: 

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

Akeem
I tried this also. Same exact error. It's strange that 3 different approaches all come up with the same error. I wonder if it has to do with my routes config file:routes.abc.route = "abc/buy/:id/*"routes.abc.defaults.controller = "deal"routes.abc.defaults.action = "buy"routes.abc.reqs.id = "\d+"
A: 

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.

A: 

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

Josh Ribakoff
A: 

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();
   }
}
Vaidas Zilionis
A: 

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())

kotresh kumar