函数名称:Yaf_Controller_Abstract::forward()
适用版本:Yaf框架2.2.0及以上版本
函数说明:Yaf_Controller_Abstract::forward()方法用于在当前请求的控制器中,将请求转发到另一个控制器或动作,实现请求的内部重定向。
用法:forward(string $controller, string $action, array $parameters = array())
参数:
- $controller(必需):要转发到的控制器名称。
- $action(必需):要转发到的动作名称。
- $parameters(可选):要传递给目标控制器的参数。默认值为一个空数组。
返回值:无返回值。
示例:
class IndexController extends Yaf_Controller_Abstract
{
public function indexAction()
{
// 执行一些逻辑操作
// 转发到另一个控制器的index动作
$this->forward('other', 'index', ['param1' => 'value1', 'param2' => 'value2']);
}
}
class OtherController extends Yaf_Controller_Abstract
{
public function indexAction()
{
// 获取传递过来的参数
$param1 = $this->getRequest()->getParam('param1');
$param2 = $this->getRequest()->getParam('param2');
// 执行一些逻辑操作
// 做出响应
}
}
在上述示例中,当执行IndexController的indexAction时,会执行一些逻辑操作后调用forward()方法将请求转发到OtherController的indexAction,并传递了两个参数param1和param2。在OtherController的indexAction中,可以通过getRequest()方法获取传递过来的参数并进行相应的处理。