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.

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

报告相同问题?

悬赏问题

  • ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式
  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案