Zend Framework重定向方法概述
本文对Zend Framework框架中常用的重定向技术进行了归纳整理,旨在为相关开发工作提供参考依据。
一、Render机制
当未显式指定渲染目标时
系统默认生成的结果为:{当前Module}/{当前Controller}/{当前Action}.phtml
$this->render('bar');
若指定特定视图文件,结果变为:{当前Module}/{当前Controller}/bar.phtml
二、Forward转发机制
$this->_forward('bar');
仅指定动作名时,结果为:{当前Module}/{当前Controller}/bar
$this->_forward('bar', 'foo');
指定动作与控制器时,结果为:{当前Module}/foo/bar
$this->_forward('bar', 'foo', 'hoge');
进一步指定模块时,结果为:hoge/foo/bar
$params = array(
'a' => '1',
'b' => '2'
);
$this->_forward('bar', 'foo', 'hoge', $params);
在包含额外参数的情况下,结果为:/hoge/foo/bar/a/1/b/2
三、Redirect重定向机制
$this->_redirect('/hoge');
相对路径重定向示例结果为:/hoge
$this->_redirect('/hoge/foo');
多级相对路径重定向示例结果为:/hoge/foo
$this->_redirect('/hoge/foo/bar');
更深层级的相对路径重定向示例结果为:/hoge/foo/bar
$this->_redirect('http://localhost/hoge/foo/bar');
绝对URL重定向示例结果为:http://localhost/hoge/foo/bar
$this->_redirect('http://localhost/hoge/foo/bar?a=1&b=2');
带查询参数的绝对URL重定向示例结果为:http://localhost/hoge/foo/bar?a=1&b=2
四、特殊场景处理
针对不使用Layout的情况
其输出表现如下:
$this->_helper->layout()->disableLayout();
针对不使用View的情况
其输出表现如下:
$this->_helper->viewRenderer->setNoRender();
希望上述关于Zend Framework框架PHP程序设计的总结内容能对大家有所帮助。