经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
基于thinkphp6.0的success、error实现方法
来源:jb51  时间:2019/11/5 12:44:46  对本文有异议

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

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

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

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

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

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

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

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

  1. use think\exception\HttpResponseException;
  2. use think\Response;
  3. ……
  4. /**
  5. * 操作成功跳转的快捷方法
  6. * @access protected
  7. * @param mixed $msg 提示信息
  8. * @param string $url 跳转的URL地址
  9. * @param mixed $data 返回的数据
  10. * @param integer $wait 跳转等待时间
  11. * @param array $header 发送的Header信息
  12. * @return void
  13. */
  14. protected function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
  15. {
  16. if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
  17. $url = $_SERVER["HTTP_REFERER"];
  18. } elseif ($url) {
  19. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
  20. }
  21. $result = [
  22. 'code' => 1,
  23. 'msg' => $msg,
  24. 'data' => $data,
  25. 'url' => $url,
  26. 'wait' => $wait,
  27. ];
  28. $type = $this->getResponseType();
  29. // 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式
  30. if ('html' == strtolower($type)) {
  31. $type = 'view';
  32. }
  33. $response = Response::create($result, $type)->header($header)->options(['jump_template' => app()->config->get('app.dispatch_success_tmpl')]);
  34. throw new HttpResponseException($response);
  35. }
  36. /**
  37. * 操作错误跳转的快捷方法
  38. * @access protected
  39. * @param mixed $msg 提示信息
  40. * @param string $url 跳转的URL地址
  41. * @param mixed $data 返回的数据
  42. * @param integer $wait 跳转等待时间
  43. * @param array $header 发送的Header信息
  44. * @return void
  45. */
  46. protected function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
  47. {
  48. if (is_null($url)) {
  49. $url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);';
  50. } elseif ($url) {
  51. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
  52. }
  53. $result = [
  54. 'code' => 0,
  55. 'msg' => $msg,
  56. 'data' => $data,
  57. 'url' => $url,
  58. 'wait' => $wait,
  59. ];
  60. $type = $this->getResponseType();
  61. if ('html' == strtolower($type)) {
  62. $type = 'view';
  63. }
  64. $response = Response::create($result, $type)->header($header)->options(['jump_template' => app()->config->get('app.dispatch_success_tmpl')]);
  65. throw new HttpResponseException($response);
  66. }

总结

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号