我不知道如何将标头添加到来自中间件的响应中。我都用过->header(...)
,->headers->set(...)
但都给出了错误。你是怎么做到的?
首先我尝试了
public function handle($request, Closure $next) {
$response = $next($request);
$response->headers->set('refresh', '5;url=' . route('foo'));
return $response;
}
这与中的相同Illuminate\Http\Middleware\FrameGuard.php
,但是给出了
调用非对象上的成员函数set()
第二我尝试了
public function handle($request, Closure $next) {
$response = $next($request);
$response->header('refresh', '5;url=' . route('foo'));
return $response;
}
但这给
方法[header]在视图上不存在。
那么,如何从中间件添加标题呢?
我通过使用response
助手解决了这个问题。
use Illuminate\Http\RedirectResponse;
$response = $next($request);
$response = $response instanceof RedirectResponse ? $response : response($response);
return $response->header('refresh', '5;url=' . route('foo'));
我所有其他中间件似乎都可以正常运行,所以我想这很好。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句