CakePHP中render,redirect,dispatch 的差别

WBOY
Release: 2016-06-13 12:33:26
Original
836 people have browsed it

CakePHP中render,redirect,dispatch 的区别
http://blog.csdn.net/kunshan_shenbin/article/details/6221219
在CakePHP,跳转经常用的三个函数,render,redirect,dispatch
1、render函数
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');
}
2、redirect函数
redirect
    string $url
用户重定向,通过此方法告诉你的用户应该继续访问什么地方。这里传入的URL参数可以是一个Cake内部URL,也可以是一个完整的URL(http://...)。此方法是把url发送的浏览,然后重新请求。
3、dispatch函数
dispatch函数是在Dispatcher类中,他有
$Dispatcher->dispatch($url);
在这里调度器会解析url得到相关的参数(其中会调用到比较多的动作包括route来解析这个url)转发到对应的控制器,最后将控制权转交给相关的控制器中的方法。这个方法主要是跳转到其他Controller中,同一个Controller跳转用render。
注意:用dispatch跳转的时候再结束的时候要调用exit;

App::import('Core', array('Dispatcher'));
......
$dispatcher = new Dispatcher();
$dispatcher->dispatch($this->request,$this->response,array("controller"=>"admins","action"=>"site"));
        exit();
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!