douzai3399 2015-08-09 23:55
浏览 54
已采纳

laravel - 从http请求中获取参数

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 request:

    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:

$router->get('/api/questions/check/(:any)', 'ApiController@getAnswer');

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).

How can I make this request?


[200]: /api/questions/check?choices= choice+1 &choices= choice+2 &choices= choice+3 &id=1

  • 写回答

1条回答 默认 最新

  • donglisi8644 2015-08-09 23:58
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。