php laravel - 从 http 请求中获取参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31909964/
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 - get parameters from http request
提问by Growler
I want to pass in multiple parameters from my Angular app to my Laravel API, namely the id
and choices
array supplied by user.
我想将多个参数从我的 Angular 应用程序传递到我的 Laravel API,即用户提供的id
andchoices
数组。
Angular:
角度:
http request:
http请求:
verifyAnswer: function(params) {
return $http({
method: 'GET',
url: 'http://localhost:8888/api/questions/check',
cache: true,
params: {
id: params.question_id,
choices: params.answer_choices
}
});
Laravel 5:
拉维尔5:
routes.php:
路线.php:
$router->get('/api/questions/check/(:any)', 'ApiController@getAnswer');
ApiController.php:
ApiController.php:
public function getAnswer(Request $request) {
die(print_r($request));
}
I thought I should use :any
within my URI to indicate I'll be passing in an arbitrary amount of parameters of various data structure (id is a number, choices is an array of choices).
我想我应该:any
在我的 URI 中使用来表示我将传入任意数量的各种数据结构的参数(id 是一个数字,choices 是一个选项数组)。
How can I make this request?
我怎样才能提出这个要求?
[200]: /api/questions/check?choices= choice+1 &choices= choice+2 &choices= choice+3 &id=1
[200]:/api/questions/check?choices=choice+1 &choices=choice+2 &choices=choice+3 &id=1
回答by baao
Change this:
改变这个:
$router->get('/api/questions/check/(:any)', 'ApiController@getAnswer');
to
到
$router->get('/api/questions/check', 'ApiController@getAnswer');
And fetch the values with
并获取值
echo $request->id;
echo $request->choices;
in your controller. There is no need to specify that you will receive parameters, they will all be in $request
when you inject Request
to your method.
在您的控制器中。无需指定您将接收参数,它们将在$request
您注入Request
到您的方法时全部包含在内。