关于 Laravel 请求的问题
后台使用了 Laravel-admin, 然后在控制器中注入了一个新增的 request 类(CRequest),
然后添加表单的时候,如果符合了条件则会添加成功。
但是如果不符合条件会报出 Trying to get property 'headers' of non-object。
追踪了一下发现是 csrf 那个中间件报出来的错误。
protected function addCookieToResponse($request, $response)
{
$config = config('session');
$response->headers->setCookie(
new Cookie(
'XSRF-TOKEN', $request->session()->token(), $this->availableAt(60 * $config['lifetime']),
$config['path'], $config['domain'], $config['secure'], false, false, $config['same_site'] ?? null
)
);
return $response;
}
这里获取了 $response 的 headers,我打印出了 $response,是一个数组,就是 reqeust 类里面的报错信息数组。
之前一开始我的代码也是正确的,然后我找同事的代码来看(他是 clone 我旧的代码),他能正确执行代码,打印了一下这个 $response 这个应该报出一个 jsonResponse 的对象才是正确的。
往上追 handle 方法:
public function handle($request, Closure $next)
{
if (
$this->isReading($request) ||
$this->runningUnitTests() ||
$this->inExceptArray($request) ||
$this->tokensMatch($request)
) {
return tap($next($request), function ($response) use ($request) {
if ($this->shouldAddXsrfTokenCookie()) {
$this->addCookieToResponse($request, $response);
}
});
}
throw new TokenMismatchException('CSRF token mismatch.');
}
这里 $next ($request) 出来的就是后面的 $response。
后面可能不知道改了什么东西就变成了这样,有大神了解吗?谢谢了!
推荐文章: