佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 基于thinkphp6.0的success、error實現(xiàn)方法

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

熱門標(biāo)簽:電子圍欄 Linux服務(wù)器 Mysql連接數(shù)設(shè)置 阿里云 服務(wù)器配置 團(tuán)購網(wǎng)站 銀行業(yè)務(wù) 科大訊飛語音識別系統(tǒng)

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

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

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

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

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

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

// 默認(rèn)跳轉(zhuǎn)頁面對應(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)等待時間
   * @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);
  }
  /**
   * 操作錯誤跳轉(zhuǎn)的快捷方法
   * @access protected
   * @param mixed $msg 提示信息
   * @param string $url 跳轉(zhuǎn)的URL地址
   * @param mixed $data 返回的數(shù)據(jù)
   * @param integer $wait 跳轉(zhuǎn)等待時間
   * @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實現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

您可能感興趣的文章:
  • ThinkPHP跳轉(zhuǎn)頁success及error模板實例教程
  • ThinkPHP頁面跳轉(zhuǎn)success與error方法概述
  • ThinkPHP提示錯誤Fatal error: Allowed memory size的解決方法

標(biāo)簽:衢州 廣元 江蘇 蚌埠 衡水 大理 萍鄉(xiāng) 棗莊

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《基于thinkphp6.0的success、error實現(xiàn)方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    伽师县| 习水县| 资兴市| 随州市| 安义县| 漳浦县| 定南县| 宁海县| 乐东| 桃江县| 石林| 沈阳市| 开阳县| 平舆县| 安西县| 嘉定区| 霍山县| 资阳市| 绿春县| 牡丹江市| 怀远县| 玉山县| 龙山县| 福鼎市| 宁德市| 衢州市| 南开区| 井陉县| 偏关县| 临夏市| 冕宁县| 淮南市| 台湾省| 威宁| 丹巴县| 高青县| 海宁市| 天祝| 丰镇市| 大宁县| 溧水县|