douwan4993
douwan4993
2010-06-27 23:47
浏览 14
已采纳

Zend Framework _forward到同一控制器内的其他动作

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 ?

图片转代码服务由CSDN问答提供 功能建议

如何转发到同一控制器内的其他操作,避免重复所有调度过程? \ n

示例: 如果我指向用户控制器,默认操作是此函数中的indexAction(),我使用_forwad('list')...但是所有的调度过程都会重复...而且我不这样做

什么是正确的方法?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doushen2154
    doushen2154 2010-06-28 04:04
    已采纳

    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.

    点赞 评论
  • dphs48626
    dphs48626 2010-06-28 04:27

    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()
        {
            // ...
        }
    }
    
    点赞 评论
  • dongna2498
    dongna2498 2013-06-01 13:32

    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()
    {
        ...
    }
    
    点赞 评论
  • dongping5230
    dongping5230 2017-04-26 13:41

    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
    
    点赞 评论

相关推荐