温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在thinkphp6.0中实现success、error

发布时间:2021-05-31 17:13:57 来源:亿速云 阅读:300 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关怎么在thinkphp6.0中实现success、error,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

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有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI