基于thinkphp6.0的success、error實(shí)現(xiàn)方法
最近把項(xiàng)目升級(jí)到tp6.0,一開(kāi)始比較順利,安裝文檔升級(jí),但是升級(jí)指導(dǎo)指出:
系統(tǒng)不再提供基礎(chǔ)控制器類(lèi) think\Controller ,原來(lái)的 success 、 error 、 redirect 和 result 方法需要自己在基礎(chǔ)控制器類(lèi)里面實(shí)現(xiàn)。
這意味著需要自己來(lái)實(shí)現(xiàn)原來(lái)的一系列的函數(shù)
我這里參考to5.1的跳轉(zhuǎn)源碼,進(jìn)行改進(jìn)得到,具體步驟如下:
1、app目錄下新建一個(gè)tpl文件夾,放入dispatch_jump.tpl文件,這個(gè)可以直接到原來(lái)的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、在基類(lèi)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 行為里通過(guò)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ì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- ThinkPHP6使用JWT+中間件實(shí)現(xiàn)Token驗(yàn)證實(shí)例詳解
- Thinkphp6 配置并使用redis圖文詳解
- thinkphp6中Redis 的基本使用方法詳解
- ThinkPHP6.0前置、后置中間件區(qū)別
- ThinkPHP6.0 重寫(xiě)URL去掉Index.php的解決方法
- thinkphp3.2框架集成QRcode生成二維碼的方法分析
- Thinkphp使用Zxing擴(kuò)展庫(kù)解析二維碼內(nèi)容圖文講解
- Thinkphp3.2.3整合phpqrcode生成帶logo的二維碼
- ThinkPHP6使用最新版本Endroid/QrCode生成二維碼的方法實(shí)例
相關(guān)文章
PHP獲取瀏覽器信息類(lèi)和客戶(hù)端地理位置的2個(gè)方法
這篇文章主要介紹了PHP獲取瀏覽器信息類(lèi)和客戶(hù)端地理位置的2個(gè)方法,需要的朋友可以參考下2014-04-04
Laravel5.5 手動(dòng)分頁(yè)和自定義分頁(yè)樣式的簡(jiǎn)單實(shí)現(xiàn)
今天小編就為大家分享一篇Laravel5.5 手動(dòng)分頁(yè)和自定義分頁(yè)樣式的簡(jiǎn)單實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
Zend Framework框架db類(lèi)的分頁(yè)示例分享
這篇文章主要介紹了Zend Framework框架db類(lèi)的分頁(yè)示例,代碼很簡(jiǎn)單,大家看一下注釋就可以使用了2014-03-03
Laravel框架實(shí)現(xiàn)的上傳圖片到七牛功能詳解
這篇文章主要介紹了Laravel框架實(shí)現(xiàn)的上傳圖片到七牛功能,結(jié)合實(shí)例形式詳細(xì)分析了七牛擴(kuò)展包相關(guān)安裝、配置及操作Laravel上傳圖片到七牛的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-09-09
Laravel 5框架學(xué)習(xí)之Laravel入門(mén)和新建項(xiàng)目
這篇文章主要是Laravel5框架學(xué)習(xí)系列的第一篇文章,跟其他開(kāi)篇文章一樣,我們來(lái)學(xué)習(xí)下Laravel入門(mén)和新建項(xiàng)目,十分的簡(jiǎn)單易懂,有需要的小伙伴可以參考下。2015-04-04
關(guān)于php中的json_encode()和json_decode()函數(shù)的一些說(shuō)明
下面小編就為大家?guī)?lái)一篇關(guān)于php中的json_encode()和json_decode()函數(shù)的一些說(shuō)明。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11

