基于thinkphp6.0的success、error实现方法


Posted in PHP onNovember 05, 2019

最近把项目升级到tp6.0,一开始比较顺利,安装文档升级,但是升级指导指出:

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

这意味着需要自己来实现原来的一系列的函数

我这里参考to5.1的跳转源码,进行改进得到,具体步骤如下:

1、app目录下新建一个tpl文件夹,放入dispatch_jump.tpl文件,这个可以直接到原来的tp5中copy

2、在config文件夹的app.php中添加配置模板文件的路径

// 默认跳转页面对应的模板文件
  'dispatch_success_tmpl' => app()->getRootPath() . '/app/tpl/dispatch_jump.tpl',
  'dispatch_error_tmpl'  => app()->getRootPath() . '/app/tpl/dispatch_jump.tpl',

3、在基类BaseController中添加下面的代码:

use think\exception\HttpResponseException;
use think\Response;
……
  /**
   * 操作成功跳转的快捷方法
   * @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 : $this->app->route->buildUrl($url);
   }
   $result = [
   'code' => 1,
   'msg' => $msg,
   'data' => $data,
   'url' => $url,
   'wait' => $wait,
   ];
   $type = $this->getResponseType();
   // 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式
   if ('html' == strtolower($type)) {
   $type = 'view';
   }
   $response = Response::create($result, $type)->header($header)->options(['jump_template' => app()->config->get('app.dispatch_success_tmpl')]);
   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 ('html' == strtolower($type)) {
   $type = 'view';
   }
   $response = Response::create($result, $type)->header($header)->options(['jump_template' => app()->config->get('app.dispatch_success_tmpl')]);
   throw new HttpResponseException($response);
  }

总结

以上所述是小编给大家介绍的基于thinkphp6.0的success、error实现方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

PHP 相关文章推荐
繁体中文转换为简体中文的PHP函数
Oct 09 PHP
一个更简单的无限级分类菜单代码
Jan 16 PHP
php获取mysql数据库中的所有表名的代码
Apr 23 PHP
解析如何修改phpmyadmin中的默认登陆超时时间
Jun 25 PHP
zf框架的Filter过滤器使用示例
Mar 13 PHP
PHP中echo和print的区别
Aug 28 PHP
smarty中js的调用方法示例
Oct 27 PHP
PHP中创建图像并绘制文字的例子
Nov 19 PHP
Codeigniter框架实现获取分页数据和总条数的方法
Dec 05 PHP
php精确的统计在线人数的方法
Oct 21 PHP
[原创]PHP获取数组表示的路径方法分析【数组转字符串】
Sep 01 PHP
详解PHP序列化和反序列化原理
Jan 15 PHP
php实现JWT(json web token)鉴权实例详解
Nov 05 #PHP
详解Laravel服务容器的绑定与解析
Nov 05 #PHP
php+laravel依赖注入知识点总结
Nov 04 #PHP
PHP保存Base64图片base64_decode的问题整理
Nov 04 #PHP
详解laravel passport OAuth2.0的4种模式
Nov 04 #PHP
laravel返回统一格式错误码问题
Nov 04 #PHP
php 中self,this的区别和操作方法实例分析
Nov 04 #PHP
You might like
从零开始学YII2框架(三)扩展插件yii2-gird
2014/08/20 PHP
ThinkPHP实现递归无级分类――代码少
2015/07/29 PHP
PHP将字符串首字母大小写转换的实例
2017/01/21 PHP
[原创]php正则删除html代码中class样式属性的方法
2017/05/24 PHP
Yii2.0建立公共方法简单示例
2019/01/29 PHP
jquery 问答知识整理
2010/02/11 Javascript
A标签中通过href和onclick传递的this对象实现思路
2013/04/19 Javascript
javascript实现根据身份证号读取相关信息
2014/12/17 Javascript
轻松创建nodejs服务器(10):处理POST请求
2014/12/18 NodeJs
JavaScript DSL 流畅接口(使用链式调用)实例
2015/03/15 Javascript
三个js循环的关键字示例(for与while)
2016/02/16 Javascript
Web开发必知Javascript技巧大全
2016/02/23 Javascript
值得分享和收藏的Bootstrap学习教程
2016/05/12 Javascript
JS 根据子网掩码,网关计算出所有IP地址范围示例
2020/04/23 Javascript
angular4实现tab栏切换的方法示例
2017/10/21 Javascript
JS实现图片轮播效果实例详解【可自动和手动】
2019/04/04 Javascript
vue-cli 项目打包完成后运行文件路径报错问题
2019/07/19 Javascript
微信小程序实现一张或多张图片上传(云开发)
2019/09/25 Javascript
js实现图片上传到服务器和回显
2020/01/19 Javascript
vue插件--仿微信小程序showModel实现模态提示窗功能
2020/08/19 Javascript
[24:42]VP vs TNC Supermajor小组赛B组 BO3 第三场 6.2
2018/06/03 DOTA
使用Python的Zato发送AMQP消息的教程
2015/04/16 Python
Python基于回溯法子集树模板实现图的遍历功能示例
2017/09/05 Python
pygame库实现俄罗斯方块小游戏
2019/10/29 Python
python GUI库图形界面开发之PyQt5多线程中信号与槽的详细使用方法与实例
2020/03/08 Python
纯CSS实现的大小渐变、渐远效果
2014/04/15 HTML / CSS
LORAC官网:美国彩妆品牌
2019/08/27 全球购物
革命先烈的英雄事迹材料
2014/02/15 职场文书
入党思想汇报怎么写
2014/04/03 职场文书
医德医风演讲稿
2014/05/20 职场文书
高中校园广播稿3篇
2014/09/29 职场文书
医生个人年终总结
2015/02/28 职场文书
就业推荐表自我评价范文
2015/03/02 职场文书
2015年清明节演讲稿范文
2015/03/17 职场文书
2015大学生暑期实习报告
2015/07/13 职场文书
使用Nginx的访问日志统计PV与UV
2022/05/06 Servers