## 頁面跳轉
操作完成后,成功或失敗頁面跳轉可以使用 控制器基類的success,error方法:
#### success方法
~~~
/**
* 操作成功跳轉的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @param string $url 跳轉的URL地址
* @param mixed $data 返回的數據
* @param integer $wait 跳轉等待時間
* @param array $header 發送的Header信息
* @return void
*/
protected function success($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
~~~
常用方式:
~~~
//默認跳轉到$_SERVER['HTTP_REFERER']
$this->success('添加成功');
//默認跳轉到article/index
$this->success('添加成功',url('article/index'));
//默認跳轉到article/index,并設置數據
$this->success('添加成功',url('article/index'),['id'=>1]);
~~~
#### error方法
~~~
/**
* 操作錯誤跳轉的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @param string $url 跳轉的URL地址
* @param mixed $data 返回的數據
* @param integer $wait 跳轉等待時間
* @param array $header 發送的Header信息
* @return void
*/
protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
~~~
常用方式:
~~~
//默認跳轉到javascript:history.back(-1);
$this->error('添加失敗');
//默認跳轉到article/index
$this->error('添加失敗',url('article/index'));
//默認跳轉到article/index,并設置數據
$this->error('添加失敗',url('article/index'),['id'=>1]);
~~~
## AJAX返回
和上面的頁面跳轉類似也使用 success,error方法,只是如果是 ajax請求會以 json格式返回
#### success方法
常用方式:
~~~
//默認跳轉到$_SERVER['HTTP_REFERER']
$this->success('添加成功');
//默認跳轉到article/index
$this->success('添加成功',url('article/index'));
//默認跳轉到article/index,并設置數據
$this->success('添加成功',url('article/index'),['id'=>1]);
~~~
返回結果:
~~~
{
"code":1,
"msg":"添加成功!",
"data":"",
"url":"",
"wait":3
}
~~~
#### error方法
常用方式:
~~~
//默認跳轉到javascript:history.back(-1);
$this->error('添加失敗');
//默認跳轉到article/index
$this->error('添加失敗',url('article/index'));
//默認跳轉到article/index,并設置數據
$this->error('添加失敗',url('article/index'),['id'=>1]);
~~~
返回結果:
~~~
{
"code":0,
"msg":"添加失敗!",
"data":"",
"url":"",
"wait":3
}
~~~
## 重定向
重定向用控制器的 redirect 方法
~~~
/**
* URL重定向
* @access protected
* @param string $url 跳轉的URL表達式
* @param array|integer $params 其它URL參數
* @param integer $code http code
* @return void
*/
protected function redirect($url, $params = [], $code = 302)
~~~
常用方式:
redirect方法的參數用法和助手函數url的用法一致(參考URL生成部分),如:
~~~
$this->redirect('Article/index', ['id' => 2]);
~~~
重定向到指定的外部URL地址 并且使用302
~~~
$this->redirect('http://www.thinkct.net',302);
~~~