简体   繁体   中英

Zend Framework _forward to other action inside same controller

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 ?

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.

We Can Also use this Helper To redirect

  $this->_helper->redirector->gotoSimple($action, $controller, $module, $params);

  $this->_helper->redirector->gotoSimple('edit'); // Example 1

  $this->_helper->redirector->gotoSimple('edit', null, null, ['id'=>1]); // Example 2 With Params

You could also create a route. For example I have in my /application/config/routes.ini a section:

; rss
routes.rss.route                = rss
routes.rss.defaults.controller  = rss
routes.rss.defaults.action      = index

routes.rssfeed.route                = rss/feed
routes.rssfeed.defaults.controller  = rss
routes.rssfeed.defaults.action      = index

Now you only need one action and that is index action but the requess rss/feed also goes there.

public function indexAction()
{
    ...
}

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()
    {
        // ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM