views:

202

answers:

2

How can i forward to other action inside the same controller avoiding repeat all dispatch proccess ?

Example: If i point to User Controller the default action is indexAction() inside this funciton i use _forwad('list') ... but all dispatch proccess are repeated.. and i dont that

Whats is the right way ?

+2  A: 

Usually, you will install routes to redirect your users to the proper (default) action, instead of the index action (read how to redirect from a given route using Zend_Router). But you can do everything manually if you really want to (however this is called "writing hacker code to achieve something dirty") directly from the controller.

Change your "view script" to be rendered, then call your action method....

// inside your controller...
public function indexAction() {
  $this->_helper->viewRenderer('foo');  // the name of the action to render instead
  $this->fooAction();  // call foo action now
}

If you tend on using this "trick" often, perhaps you may write a base controller that you extend in your application, which can simply have a method like :

abstract class My_Controller_Action extends Zend_Controller_Action {
   protected function _doAction($action) {
      $method = $action . 'Action';
      $this->_helper->viewRenderer($action);
      return $this->$method();   // yes, this is valid PHP
   }
}

Then call the method from your action...

class Default_Controller extends My_Controller_Action
   public function indexAction() {
      if ($someCondition) {
         return $this->_doAction('foo');
      }

      // execute normal code here for index action
   }
   public function fooAction() {
      // foo action goes here (you may even call _doAction() again...)
   }
}

NOTE : this is not the official way to do it, but it is a solution.

Yanick Rochon
A: 

If you don't want to re-dispatch there is no reason you can't simply call the action - it's just a function.

class Default_Controller extends My_Controller_Action
{
    public function indexAction()
    {
        return $this->realAction();
    }

    public function realAction()
    {
        // ...
    }
}
Brenton Alker