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

Vit*_*ida 3 php model-view-controller action zend-framework

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

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

什么是正确的方法?

Yan*_*hon 6

通常,您将安装路由以将用户重定向到正确的(默认)操作,而不是索引操作(读取如何使用Zend_Router从给定路由重定向).但是你可以直接从控制器中手动完成所有操作(但这称为"编写黑客代码以实现某些脏东西").

更改要渲染的"视图脚本",然后调用您的操作方法....

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

如果您经常使用这个"技巧",也许您可​​以编写一个在应用程序中扩展的基本控制器,它可以简单地使用如下方法:

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
   }
}
Run Code Online (Sandbox Code Playgroud)

然后从你的行动中调用该方法......

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...)
   }
}
Run Code Online (Sandbox Code Playgroud)

注意:这不是正式的方法,但它一个解决方案.