页面跳转

操作完成后,成功或失败页面跳转可以使用 控制器基类的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.thinkcmf.com',302);

results matching ""

    No results matching ""