关于 ThinkPHP6 无法使用 success(), error() 跳转的解决方法
2023-03-29 23:03:03
224
{{single.collect_count}}

项目场景:

对于在 ThinkPHP3,ThinkPHP5 中习惯了使用 success(), error() 跳转的小伙伴,本文将介绍如何在 ThinkPHP6 中使用这些跳转。

问题描述:

在 ThinkPHP6 中使用 诸如 $this->error('验证码错误'); 的提示中,会发现不能用了,原来是官方把 success,error 这些跳转给取消了。 在ThinkPHP官方发布的 ThinkPHP6完全开发手册 - 附录 - 升级指导 中,给出了如下说明:

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

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


原因分析:

提示:这里填写问题的分析:
例如:Handler 发送消息有两种方式,分别是 Handler.obtainMessage()和 Handler.sendMessage(),其中 obtainMessage 方式当数据量过大时,由于 MessageQuene 大小也有限,所以当 message 处理不及时时,会造成先传的数据被覆盖,进而导致数据丢失。


解决方案:

下面本文将给出如何在 ThinkPHP6 中继续使用 t h i s − > s u c c e s s ( ) ,this->success(),this>success(),this->error() 这些跳转。

1、准备跳转模板文件

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

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

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

在这里插入图片描述
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',

3、修改基础控制器类

位置在 /app/BaseController.php

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

//// 下面2行,为了使用旧版的 success error redirect 跳转//use think\exception\HttpResponseException;use think\facade\Request;

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

//// 以下为新增,为了使用旧版的 success error redirect 跳转start///** * 操作成功跳转的快捷方法 * @access protected * @parammixed $msg 提示信息 * @paramstring$url 跳转的URL地址 * @parammixed $data 返回的数据 * @paraminteger $wait 跳转等待时间 * @paramarray $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 * @parammixed $msg 提示信息 * @paramstring$url 跳转的URL地址 * @parammixed $data 返回的数据 * @paraminteger $wait 跳转等待时间 * @paramarray $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 * @paramstring $url 跳转的URL表达式 * @paramarray|integer$params 其它URL参数 * @paraminteger$code http code * @paramarray$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('请输入验证码');

结果如图所示:
在这里插入图片描述

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}