基于thinkphp6.0的success、error實(shí)現(xiàn)方法
最近把項(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)注明出處,謝謝!
- ThinkPHP6使用JWT+中間件實(shí)現(xiàn)Token驗(yàn)證實(shí)例詳解
- Thinkphp6 配置并使用redis圖文詳解
- thinkphp6中Redis 的基本使用方法詳解
- ThinkPHP6.0前置、后置中間件區(qū)別
- ThinkPHP6.0 重寫URL去掉Index.php的解決方法
- thinkphp3.2框架集成QRcode生成二維碼的方法分析
- Thinkphp使用Zxing擴(kuò)展庫(kù)解析二維碼內(nèi)容圖文講解
- Thinkphp3.2.3整合phpqrcode生成帶logo的二維碼
- ThinkPHP6使用最新版本Endroid/QrCode生成二維碼的方法實(shí)例
相關(guān)文章
php版微信公眾平臺(tái)實(shí)現(xiàn)預(yù)約提交后發(fā)送email的方法
這篇文章主要介紹了php版微信公眾平臺(tái)實(shí)現(xiàn)預(yù)約提交后發(fā)送email的方法,結(jié)合實(shí)例形式分析了php微信公眾平臺(tái)郵件發(fā)送的相關(guān)操作技巧,需要的朋友可以參考下2016-09-09PHP+MySQL投票系統(tǒng)的設(shè)計(jì)和實(shí)現(xiàn)分享
php投票系統(tǒng)的設(shè)計(jì)和實(shí)現(xiàn),需要的朋友可以參考下代碼與數(shù)據(jù)庫(kù)結(jié)果設(shè)計(jì),最后腳本之家提供了源碼下載2012-09-09