从中间件中的响应对象获取响应正文

问题描述 投票:0回答:2

我有一个中间件来记录所有请求和响应(针对 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;
}

但是该对象不包含主体。它只有状态和标题信息。 您能帮我获取回复正文吗

php laravel laravel-5
2个回答
2
投票

这是调试后的 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 content”取代。您可以调用
$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;
    }
}

希望有帮助。祝你有美好的一天!


1
投票

我将在这里回复以分享更大的代码而不是评论。因此,根据文档,它应该位于

$routeMiddleware
$middleware
中,因为如果您打开框架
Kernel
,您将看到以下内容:

$middlewares = $this->app->shouldSkipMiddleware() ? [] : array_merge(
        $this->gatherRouteMiddleware($request),
        $this->middleware
    );

在这些上调用

terminate
函数。

© www.soinside.com 2019 - 2024. All rights reserved.