基于thinkphp6.0的success、error實(shí)現(xiàn)方法
最近把項目升級到tp6.0,一開始比較順利,安裝文檔升級,但是升級指導(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目錄下新建一個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實(shí)現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
- ThinkPHP6使用JWT+中間件實(shí)現(xiàn)Token驗證實(shí)例詳解
- Thinkphp6 配置并使用redis圖文詳解
- thinkphp6中Redis 的基本使用方法詳解
- ThinkPHP6.0前置、后置中間件區(qū)別
- ThinkPHP6.0 重寫URL去掉Index.php的解決方法
- thinkphp3.2框架集成QRcode生成二維碼的方法分析
- Thinkphp使用Zxing擴(kuò)展庫解析二維碼內(nèi)容圖文講解
- Thinkphp3.2.3整合phpqrcode生成帶logo的二維碼
- ThinkPHP6使用最新版本Endroid/QrCode生成二維碼的方法實(shí)例
相關(guān)文章
Laravel5.5 手動分頁和自定義分頁樣式的簡單實(shí)現(xiàn)
今天小編就為大家分享一篇Laravel5.5 手動分頁和自定義分頁樣式的簡單實(shí)現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
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入門和新建項目
這篇文章主要是Laravel5框架學(xué)習(xí)系列的第一篇文章,跟其他開篇文章一樣,我們來學(xué)習(xí)下Laravel入門和新建項目,十分的簡單易懂,有需要的小伙伴可以參考下。2015-04-04
關(guān)于php中的json_encode()和json_decode()函數(shù)的一些說明
下面小編就為大家?guī)硪黄P(guān)于php中的json_encode()和json_decode()函數(shù)的一些說明。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11

