laravel - 从http请求中获取参数 [英] laravel - get parameters from http request
问题描述
我想将我的Angular应用程序中的多个参数传递给我的Laravel API,即 id
和选项
用户提供的数组。
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:
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:
routes.php:
routes.php:
$router->get('/api/questions/check/(:any)', 'ApiController@getAnswer');
ApiController.php:
ApiController.php:
public function getAnswer(Request $request) {
die(print_r($request));
}
我以为我应该使用:任何$在我的URI中c $ c>表示我将传递各种数据结构的任意数量的参数(id是数字,选项是一个选择数组)。
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).
我该如何提出这个要求?
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
推荐答案
改变这个:
$router->get('/api/questions/check/(:any)', 'ApiController@getAnswer');
到
$router->get('/api/questions/check', 'ApiController@getAnswer');
并使用
echo $request->id;
echo $request->choices;
。当您注入请求
时,无需指定您将接收参数,它们都将在 $ request
中你的方法。
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.
这篇关于laravel - 从http请求中获取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!