php – Zend Framework _forward到同一控制器内的其他动作
发布时间:2020-12-13 21:37:38 所属栏目:PHP教程 来源:网络整理
导读:如何转发到同一控制器内的其他操作,避免重复所有调度过程? 例: 如果我指向用户控制器默认操作是indexAction()在这个函数内我使用_forwad(‘list’)…但是所有的调度过程都重复了…我不这样做 什么是正确的方法? 解决方法 通常,您将安装路由以将用户重定向
如何转发到同一控制器内的其他操作,避免重复所有调度过程?
例: 什么是正确的方法? 解决方法
通常,您将安装路由以将用户重定向到正确的(默认)操作,而不是索引操作(读取如何使用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 } 如果您经常使用这个“技巧”,也许您可??以编写一个在应用程序中扩展的基本控制器,它可以简单地使用如下方法: 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 } } 然后从你的行动中调用该方法…… 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...) } } 注意:这不是正式的方法,但它是一个解决方案. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |