我有一个中间件来记录所有的请求和响应(用于API)。但是终止方法响应对象中未显示响应。
class Logger
{
public function handle($request, Closure $next)
{
return $next($request);
}
public function terminate($request, $response)
{
Log::info('ApiLog done===========================');
Log::info('URL: ' . $request->fullUrl());
Log::info('Method: ' . $request->getMethod());
Log::info('IP Address: ' . $request->getClientIp());
Log::info("Data: ",[$request->all()]);
// Log::info("Query String: ", [$request->query()]);
Log::info("Response".$response->getContent());
}
}
但是$ response-> getContent()返回null。 首先,我尝试仅使用处理请求的句柄并获取响应,然后使用
进行记录public function handle($request, Closure $next)
{
$response = $next($request);
Log::response("",[$response])
return $response;
}
但是对象不包含主体。它仅具有状态和标头信息。 您能帮我得到回复正文
答案 0 :(得分:1)
我将在此处回复以分享更大的代码,而不是发表评论。因此,根据文档,它应该位于REPORT.
TYPES: BEGIN OF ty_result,
vbeln TYPE vbeln,
cnt TYPE i.
TYPES: END OF ty_result.
DATA: lt_headers TYPE SORTED TABLE OF ty_result WITH UNIQUE KEY vbeln,
lv_tabix TYPE sy-tabix VALUE 1.
"get the headers
SELECT vbeln FROM vbak UP TO 100 ROWS INTO CORRESPONDING FIELDS OF TABLE lt_headers.
"get corresponding items
SELECT vbeln, posnr FROM vbap FOR ALL ENTRIES IN @lt_headers
WHERE vbeln EQ @lt_headers-vbeln
ORDER BY vbeln, posnr
INTO TABLE @DATA(lt_items).
LOOP AT lt_headers ASSIGNING FIELD-SYMBOL(<h>).
LOOP AT lt_items FROM lv_tabix ASSIGNING FIELD-SYMBOL(<i>).
IF <i>-vbeln NE <h>-vbeln.
lv_tabix = sy-tabix.
EXIT.
ELSE.
<h>-cnt = <h>-cnt + 1.
ENDIF.
ENDLOOP.
ENDLOOP.
BREAK-POINT.
或$routeMiddleware
中,因为如果您打开框架$middleware
,则会看到以下内容:
Kernel
在这些$middlewares = $this->app->shouldSkipMiddleware() ? [] : array_merge(
$this->gatherRouteMiddleware($request),
$this->middleware
);
函数上被调用。
答案 1 :(得分:0)
这是来自调试的HttpResponse的print_r
:
Illuminate\Http\Response Object
(
[headers] => Symfony\Component\HttpFoundation\ResponseHeaderBag Object
(
[computedCacheControl:protected] => Array
(
[no-cache] => 1
[private] => 1
)
[cookies:protected] => Array
(
)
[headerNames:protected] => Array
(
[cache-control] => Cache-Control
[date] => Date
[content-type] => Content-Type
)
[headers:protected] => Array
(
[cache-control] => Array
(
[0] => no-cache, private
)
[date] => Array
(
[0] => Thu, 04 Oct 2018 14:17:16 GMT
)
[content-type] => Array
(
[0] => text/html; charset=UTF-8
)
)
[cacheControl:protected] => Array
(
)
)
[content:protected] => foobar content
[version:protected] => 1.1
[statusCode:protected] => 200
[statusText:protected] => OK
[charset:protected] =>
[original] => foobar content
[exception] =>
)
属性content:protected
因其大而被“ foobar内容”所代替。您可以调用$response->setContent('foobar content');
来设置内容值,或者调用$response->getContent();
来接收内容值。
如果$response->getContent();
返回null,则必须检查$ response的对象类型。也许不是\Illuminate\Http\Response
。
这是我的测试中间件:
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
/** @var Illuminate\Http\Response $response */
$response = $next($request);
$foo = $response->getContent();
return $response;
}
}
希望有帮助。祝你有美好的一天!