0
点赞
收藏
分享

微信扫一扫

CakePHP中render,redirect,dispatch 的区别



在CakePHP,跳转经常用的三个函数,render,redirect,dispatch
[b]1、render函数[/b]
public function render($action = null, $layout = null, $file = null)
render
string $action
string $layout
string $file
render渲染视图,你也许不会经常使用这个方法,因为render方法是在controller action结束时自动被调用的,输出按action名字命令的view。同时,你也可以在controller逻辑里的任意位置调用来这个方法输出视图;如果在Controller中逻辑调用的时候,如果要求跳转当前Controller中的其他页面可以这样使用,例如:
在delete函数中删除成功后,跳转到列表页面index。可以这样写

public function delete(){

$this->index();

$this->render(null, null, 'index');

}

[b]2、redirect函数[/b]

redirect

string $url


用户重定向,通过此方法告诉你的用户应该继续访问什么地方。这里传入的URL参数可以是一个Cake内部URL,也可以是一个完整的URL(http://...)。此方法是把url发送的浏览,然后重新请求。
[b]3、dispatch函数[/b]
dispatch函数是在Dispatcher类中,他有
$Dispatcher->dispatch($url);
在这里调度器会解析url得到相关的参数(其中会调用到比较多的动作包括route来解析这个url)转发到对应的控制器,最后将控制权转交给相关的控制器中的方法。这个方法[color=darkblue]主要是跳转到其他Controller中,同一个Controller跳转用render[/color]。
注意:用dispatch跳转的时候再结束的时候要调用exit;

App::import('Core', array('Dispatcher'));
......
$dispatcher = new Dispatcher();
$dispatcher->dispatch($this->request,$this->response,array("controller"=>"admins","action"=>"site"));
exit();

举报

相关推荐

0 条评论