Zend Framework中Controller之间action跳转方式

http://qq398705749.iteye.com/blog/916413

Zend Framework中Controller之间action跳转方式 
Views+ Zend Framework (ZF) 是一个开放源代码的 PHP5 开发框架,可用于来开 发 web 程序和服务。ZF 100%用面向对象代码实现。 ZF中的组件非常独立,每个组件几乎不依赖于其他组件。这样的松耦合结构可以让开发者独立 使用组件。 我们常称此为 “use-at-will”设计。 
zf从一个controller的action跳转执行另一个controller action方法怎么操作? 

帮助1 $this->_forward($action, $controller = null, $module =   null, array $params = null) 
方法的参数依次是跳转到的方法名,控制器名,模块名和其它的参数 
比如要跳转到myzf/admin/index/ 这个地址,假如myzf是项目文件,那么控制器名便是admin,方法名是index 
要跳转到这个地址,可以像下面这样写 

帮助1 $this->_forward('index', 'admin'); 
假如你使用了多模块设置的话同时还可以指定跳转到那个module下的控制器和方法,如果还需要传递额外的参数,在参数$params中指定,比如下面的 例子 

帮助1 $this->_forward('test','test','test', array('param'=>'value','param2'=>'value2') 
在非controller里 没有_forward这方法,所以不能实现., 
另自己网上找到方法,在非controller里,可以用 

帮助123 $request->setControllerName('login');  $request->setActionName('index');  $request->setParam('redirect','/myzf/admin/index/'); 
$this->_redirect(‘/controller/action’) 会向浏览器发出一个 “Location: /controller/action” 这样的 header ,致使浏览器再产生一个 URL 为 /controller/action 的 web 请求 

$this->_forward() 却不是这样,它不依赖于浏览器,而是在执行完 actionA 后在继续执行一个 actionB,这完全是在服务端完成,中间没有跟浏览器打交道过程。 

对用户来说,$this->_forward() 时他会在地址栏上看到一个/controller/actionA,虽然还做了个 /controller/actionB 的动作,这是一个请求,两个动作。而$this->_redirect() 则是两个个请求,两个动作。 

显然两种不同的机制会造成两种不同的结果,$this->_redirect() 会将两个action 的结果分别显示在两个页面上,而 $this->_forward()  则会在一个页面同时输出两个动作的结果。 

帮助12 $this->_forward(‘index’,'zipcode’,”,’id’ =>  ‘10001′) $this->_forward(‘index’,'zipcode’,'default’,'id’ => ‘10001′) 
接收参数页面 

帮助1 $id = (int)$this->_request->getParam(‘id’, 0); 
另外再写$this->_forward()时候,参数要至少写前两个,不然就会报错: 

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 40961 bytes) in 
你的ZF项目\library\Zend\Controller\Dispatcher\Standard.php on line 123 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值