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();