Laravel - 检查请求方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43551188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - check request method
提问by Hymanchmbrln
I'm an iOS lead on an app and trying to fix some API bugs whilst our dev is 'unavailable'. I'm almost completely new to Laravel and trying to check what the request method is. I have followed some guidance from another question but have been unable to get it working:
我是一个应用程序的 iOS 负责人,并试图在我们的开发人员“不可用”时修复一些 API 错误。我对 Laravel 几乎完全陌生,并试图检查请求方法是什么。我遵循了另一个问题的一些指导,但无法使其正常工作:
public function defaults(Request $request, User $user){
$follow_ids = explode(',', env('FOLLOW_DEFAULTS'));
if ($request->isMethod('post')) {
return ['user' => $user];
}
$user->follows()->syncWithoutDetaching($follow_ids);
return ['user.follows' => $user->follows->toArray()];
}
Do you know where I might be going wrong here? Thanks in advance.
你知道我在这里可能会出错吗?提前致谢。
When the request is returned it always just seems to skip over and return ['user.follows' => $user->follows->toArray()]
当请求返回时,它似乎总是跳过并返回 ['user.follows' => $user->follows->toArray()]
回答by patricus
$request
should be an instance of Illuminate\Http\Request
. This class extends Symfony's request (Symfony\Component\HttpFoundation\Request
), which is actually where the isMethod()
method is defined.
$request
应该是 的一个实例Illuminate\Http\Request
。这个类扩展了 Symfony 的 request( Symfony\Component\HttpFoundation\Request
),它实际上isMethod()
是定义方法的地方。
Basically, given the function definition as posted, it reads "if this is a POST
request, just return the user data. if this is not a POST
request (e.g. GET
), update and return the relationship data."
基本上,给定发布的函数定义,它读取“如果这是一个POST
请求,只返回用户数据。如果这不是POST
请求(例如GET
),更新并返回关系数据。”
So, if you send a POST
request, you'll get the ['user' => $user]
response. If you send any other request method (e.g. GET
), you'll modify the follows
relationship and get the ['user.follows' => $user->follows->toArray()]
response.
所以,如果你发送一个POST
请求,你会得到['user' => $user]
响应。如果您发送任何其他请求方法(例如GET
),您将修改follows
关系并获得['user.follows' => $user->follows->toArray()]
响应。
To me, this seems backwards. I would think you'd want the POST
request to update the data, and any other request (e.g. GET
) to just return data.
对我来说,这似乎是倒退。我认为您希望POST
请求更新数据,以及任何其他请求(例如GET
)只返回数据。
If this is correct, you need to negate your isMethod
check:
如果这是正确的,您需要否定您的isMethod
支票:
if (! $request->isMethod('post')) {
return ['user' => $user];
}
More appropriately you should define separate controller actions to handle POST
vs GET
requests, but that is outside the scope of this question, and probably more than you want to get into as a temporary maintainer.
更合适的是,您应该定义单独的控制器操作来处理POST
vsGET
请求,但这超出了本问题的范围,并且可能超出了您作为临时维护者想要进入的范围。
回答by aletzo
It seems that the request is not a POST so the if
check is never true. You could echo the method name like this:
该请求似乎不是 POST,因此if
检查永远不会正确。您可以像这样回显方法名称:
$method = $request->method();
echo $method;
// or var_dump($method);