欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于thinkphp6.0的success、error實(shí)現(xiàn)方法

 更新時(shí)間:2019年11月05日 11:12:46   作者:工學(xué)1號(hào)館  
這篇文章主要介紹了基于thinkphp6.0的success、error實(shí)現(xiàn)方法,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

最近把項(xiàng)目升級(jí)到tp6.0,一開始比較順利,安裝文檔升級(jí),但是升級(jí)指導(dǎo)指出:

系統(tǒng)不再提供基礎(chǔ)控制器類 think\Controller ,原來的 success 、 error 、 redirect 和 result 方法需要自己在基礎(chǔ)控制器類里面實(shí)現(xiàn)。

這意味著需要自己來實(shí)現(xiàn)原來的一系列的函數(shù)

我這里參考to5.1的跳轉(zhuǎn)源碼,進(jìn)行改進(jìn)得到,具體步驟如下:

1、app目錄下新建一個(gè)tpl文件夾,放入dispatch_jump.tpl文件,這個(gè)可以直接到原來的tp5中copy

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

// 默認(rèn)跳轉(zhuǎn)頁(yè)面對(duì)應(yīng)的模板文件
  '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;
……
  /**
   * 操作成功跳轉(zhuǎn)的快捷方法
   * @access protected
   * @param mixed $msg 提示信息
   * @param string $url 跳轉(zhuǎn)的URL地址
   * @param mixed $data 返回的數(shù)據(jù)
   * @param integer $wait 跳轉(zhuǎn)等待時(shí)間
   * @param array $header 發(fā)送的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();
   // 把跳轉(zhuǎn)模板的渲染下沉,這樣在 response_send 行為里通過getData()獲得的數(shù)據(jù)是一致性的格式
   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);
  }
  /**
   * 操作錯(cuò)誤跳轉(zhuǎn)的快捷方法
   * @access protected
   * @param mixed $msg 提示信息
   * @param string $url 跳轉(zhuǎn)的URL地址
   * @param mixed $data 返回的數(shù)據(jù)
   * @param integer $wait 跳轉(zhuǎn)等待時(shí)間
   * @param array $header 發(fā)送的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);
  }

總結(jié)

以上所述是小編給大家介紹的基于thinkphp6.0的success、error實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論