Php+debugbar+api,react + Laravel Debugbar API 调试
一、Debugbar 安裝與配置
1、使用 Composer 安裝該擴展包:
composer require barryvdh/laravel-debugbar --dev
2、接下來運行以下命令生成此擴展包的配置文件 config/debugbar.php :
php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"
3、打開 config/debugbar.php 文件,修改如下配置:
return [
...
'capture_ajax' => false,
'inject' => false,
...
],
4、打開 app/Providers/RouteServiceProvider.php 文件,在 boot 方法里添加一條渲染路由
public function boot()
{
if ($this->app->environment('local')) {
Route::group(['prefix' => config('debugbar.route_prefix')], function () {
Route::get('render', function () {
$debugBar = debugbar();
$renderer = $debugBar->getJavascriptRenderer();
$renderer->setOpenHandlerUrl('/' . config('debugbar.route_prefix') . '/open');
$script = $renderer->render();
preg_match('/(?:
$js = $matches[1];
$jsRetryFn = "function retry(times, fn, sleep) {
if (!times) times = 1;
if (!sleep) sleep = 50;
--times;
try {
return fn();
} catch (e) {
if (!times) throw e;
if (sleep) {
setTimeout(function() {
retry(times, fn, sleep);
}, sleep);
}
}
}\n";
// sleep(1);
echo "${jsRetryFn}\nretry(50, function() {\n${js}\nwindow.phpdebugbar = phpdebugbar\n}, 200);";
exit;
});
});
}
parent::boot();
}
5、打開 app/Providers/AppServiceProvider.php 文件,在 boot 方法里添加如下代碼:
public function boot()
{
if (app()->environment('local') && request()->isJson()) {
$debugbar = debugbar();
$debugbar->sendDataInHeaders(true);
}
}
二、react 配置
1、在入口模板文件 document.ejs 載入 js 和 css 文件,并且渲染debugbar
<% if(context.env !== 'production') { %>
2、api 請求自動刷新debugbar渲染
/**
* request 網絡請求工具
* 更詳細的 api 文檔: https://github.com/umijs/umi-request
*/
import { extend } from 'umi-request';
import { notification } from 'antd';
import cookie from 'cookie';
import { getToken } from '@/utils/authority';
const codeMessage = {
200: '服務器成功返回請求的數據。',
201: '新建或修改數據成功。',
202: '一個請求已經進入后臺排隊(異步任務)。',
204: '刪除數據成功。',
400: '發出的請求有錯誤,服務器沒有進行新建或修改數據的操作。',
401: '用戶沒有權限(令牌、用戶名、密碼錯誤)。',
403: '用戶得到授權,但是訪問是被禁止的。',
404: '發出的請求針對的是不存在的記錄,服務器沒有進行操作。',
406: '請求的格式不可得。',
410: '請求的資源被永久刪除,且不會再得到的。',
422: '當創建一個對象時,發生一個驗證錯誤。',
500: '服務器發生錯誤,請檢查服務器。',
502: '網關錯誤。',
503: '服務不可用,服務器暫時過載或維護。',
504: '網關超時。',
};
/**
* 異常處理程序
*/
const errorHandler = async (error: { response: Response }): Promise => {
const { response } = error;
if (response && response.status) {
const { status, url } = response;
if (status === 401) {
// @ts-ignore https://umijs.org/zh/guide/with-dva.html#faq
window.g_app._store.dispatch({ type: 'login/logout' });
}
const errorText = codeMessage[response.status] || response.statusText;
const { message: msg } = await response.json();
notification.error({
message: `請求錯誤 ${status}: ${url}`,
description: msg || errorText,
});
const error: any = new Error(msg || errorText);
error.response = response;
throw error;
}
};
/**
* 配置request請求時的默認參數
*/
const request = extend({
prefix: '/api',
errorHandler, // 默認錯誤處理
credentials: 'include', // 默認請求是否帶上cookie
headers: {
Accept: `application/x.sheng.${API_VERSION || 'v1'}+json`, // eslint-disable-line
'Content-Type': 'application/json; charset=utf-8',
},
});
// request攔截器, 改變url 或 options.
/* eslint no-param-reassign:0 */
request.interceptors.request.use((url, options) => {
const { headers } = options;
options.headers = {
...headers,
Authorization: getToken(),
'X-XSRF-TOKEN': cookie.parse(document.cookie)['XSRF-TOKEN'],
};
return { url, options };
});
// response攔截器, 處理response
request.interceptors.response.use(response => {
/* eslint no-undef:0, valid-typeof:0 */
if (typeof phpdebugbar !== undefined) {
try {
const {
ajaxHandler: { headerName },
} = phpdebugbar;
const debugBarData = response.headers.get(headerName);
const debugBarId = response.headers.get(`${headerName}-id`);
if (debugBarData) {
const { id, data } = JSON.parse(decodeURIComponent(debugBarData));
phpdebugbar.addDataSet(data, id);
} else if (debugBarId && phpdebugbar.openHandler) {
phpdebugbar.loadDataSet(debugBarId, '(ajax)');
}
} catch (e) {
//
}
}
return response;
});
export default request;
本作品采用《CC 協議》,轉載必須注明作者和本文鏈接
總結
以上是生活随笔為你收集整理的Php+debugbar+api,react + Laravel Debugbar API 调试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux c libpcap统计流量,
- 下一篇: 如何保证进程不被杀死的几个办法