我需要记录每个api请求的响应。为此,我创建了一个中间件,并在该中间件的终止功能中使用了ResponseTrait。一切正常,直到我使用ResponseTrait的状态功能。我尝试了$ this-> status和self :: status但没有任何效果。当我使用这个函数时它会说语法错误。以下是我的中间件的代码。
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use Spatie\HttpLogger\LogWriter;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\ResponseTrait;
class logger implements LogWriter
{
use ResponseTrait;
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
return $next($request);
}
public function terminate($request)
{
$responseCode = $this->status();
}
}
答案 0 :(得分:1)
响应状态来自$next($request);
的结果,因此您需要引用它,如:
$response = $next($request);
$status = $response->status();
return $response;