php $this->error不跳转,关于 ThinkPHP6 无法使用 success(), error() 跳转的解决方法

在 ThinkPHP6 中使用 诸如 $this->error('验证码错误'); 的提示中,会发现不能用了,原来是官方把 success,error 这些跳转给取消了。

在ThinkPHP官方发布的 ThinkPHP6完全开发手册 - 附录 - 升级指导 中,给出了如下说明:

“系统不再提供基础控制器类think\Controller,原来的success、error、redirect和result方法需要自己在基础控制器类里面实现。”

“系统默认在应用目录下面提供了一个app\BaseController基础类,或者你可以直接放入你的应用里面,继承使用。”

下面本文将给出如何在 ThinkPHP6 中继续使用 $this->success(),$this->error() 这些跳转。

1、准备跳转模板文件

把旧的 ThinkPHP 框架的中的跳转模板文件复制过来

位置在 旧的框架核心文件 /thinkphp/tpl/dispatch_jump.tpl

比如放到:/public/tpl/dispatch_jump.tpl 这个位置,如图所示:

a43ac2d374a61caded2602fe44bcd27e.png

2、修改配置文件

位置在 /config/app.php 把如下2行代码加在配置文件中,如下:// 默认跳转页面对应的模板文件【新增】

'dispatch_success_tmpl' => app()->getRootPath() . '/public/tpl/dispatch_jump.tpl',

'dispatch_error_tmpl'  => app()->getRootPath() . '/public/tpl/dispatch_jump.tpl',

注意文件的路径,和第 1 步中保持一致,加好后如图所示:

7913183303d6ee8a1b38fdb8587c8849.png

3、修改基础控制器类

位置在 /app/BaseController.php

(1)在顶部加上如下2行代码,引入的文件,如下://

// 下面2行,为了使用旧版的 success error redirect 跳转

//

use think\exception\HttpResponseException;

use think\facade\Request;

加好后如图所示:

9eecb6c72f2dee14750a6a427d9e18b9.png

(2)接下来,还是在这个基础控制器类文件里进行修改,直接加入如下代码://

// 以下为新增,为了使用旧版的 success error redirect 跳转  start

//

/**

* 操作成功跳转的快捷方法

* @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 = '', string $url = null, $data = '', int $wait = 3, array $header = [])

{

if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {

$url = $_SERVER["HTTP_REFERER"];

} elseif ($url) {

$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url);

}

$result = [

'code' => 1,

'msg'  => $msg,

'data' => $data,

'url'  => $url,

'wait' => $wait,

];

$type = $this->getResponseType();

if ($type == 'html'){

$response = view($this->app->config->get('app.dispatch_success_tmpl'), $result);

} else if ($type == 'json') {

$response = json($result);

}

throw new HttpResponseException($response);

}

/**

* 操作错误跳转的快捷方法

* @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 = '', string $url = null, $data = '', int $wait = 3, array $header = [])

{

if (is_null($url)) {

$url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);';

} elseif ($url) {

$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);

}

$result = [

'code' => 0,

'msg'  => $msg,

'data' => $data,

'url'  => $url,

'wait' => $wait,

];

$type = $this->getResponseType();

if ($type == 'html'){

$response = view($this->app->config->get('app.dispatch_error_tmpl'), $result);

} else if ($type == 'json') {

$response = json($result);

}

throw new HttpResponseException($response);

}

/**

* URL重定向  自带重定向无效

* @access protected

* @param  string         $url 跳转的URL表达式

* @param  array|integer  $params 其它URL参数

* @param  integer        $code http code

* @param  array          $with 隐式传参

* @return void

*/

protected function redirect($url, $params = [], $code = 302, $with = [])

{

$response = Response::create($url, 'redirect');

if (is_integer($params)) {

$code   = $params;

$params = [];

}

$response->code($code)->params($params)->with($with);

throw new HttpResponseException($response);

}

/**

* 获取当前的response 输出类型

* @access protected

* @return string

*/

protected function getResponseType()

{

return $this->request->isJson() || $this->request->isAjax() ? 'json' : 'html';

}

//

// 以上为新增,为了使用旧版的 success error redirect 跳转  end

//

4、测试跳转效果

像旧版框架一样,比如使用:$this->error('请输入验证码');

结果如图所示:

07fa8c1299580e4f254b8cdcc9952403.png

达到了预期的效果。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值