如何设置转发请求的标头 [英] How to set headers for forwarded request
问题描述
我的控制器的方法需要设置标头,例如X-Authorization
.创建新对象(store
操作)后,我做一个正向操作以显示新创建的对象(show
操作):
My controller's methods require a header to be set, e.g. X-Authorization
. After a new object has been created (store
action), I do a forward to show the newly created object (show
action):
$request = Request::create(route('api.v1.b.show', ['booking' => 4]), 'GET');
Request::replace($request->input());
return Route::dispatch($request);
如果我禁用了授权检查,则转发正常,但否则失败. IE.标头不见了.我想将请求标头复制到转发的请求中,该标头可以通过Request::header('X-Authorization')
获得.有可能吗?
The forwarding works ok if I disable the authorization check, but it fails otherwise. ie. the header has gone. I would like to copy the request header, which I can get with Request::header('X-Authorization')
into the forwarded request. Is it possible?
我尝试做$request->header('X-Authorization', 'xxxxx')
没有成功.还曾在分派前尝试过PHP的header()
,但没有用.
I have tried without success to do $request->header('X-Authorization', 'xxxxx')
. Also tried PHP's header()
before the dispatch and didn't work.
有什么想法吗?干杯
推荐答案
好吧,我认为您需要像这样设置标头:
Ok, i think you need to set the headers like so:
$request = Request::create(route('api.v1.b.show', ['booking' => 4]), 'GET');
$request->headers->set('X-Authorization', 'xxxxx');
那是您问题的答案.
我的问题是:我们在哪里可以为每个api请求(转发)设置此标头?因为我个人有5个可与请求一起设置的标头,所以我不想重复自己.
My question is: Where can we set this headers for every api request(forwarding)? Because i personally have 5 headers to set with the request and i don't want to repeat myself.
这篇关于如何设置转发请求的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!