如何使用FOSRestBundle进行POST调用

时间:2016-07-26 08:41:10

标签: rest symfony fosrestbundle

我正在尝试使用FOSRestBunble设置RESTFUL Web服务,但是我在调​​用POST时遇到了一些问题,这是我的设置:

应用程序/配置/ routing.yml中

rest:
    type: rest
    resource: "routing_rest.yml"
    prefix: /api

应用程序/配置/ routing_rest.yml

Rest_User:
    type: rest
    resource: "@AppBundle/Resources/config/routing_rest.yml"

的appbundle /资源/配置/ routing_rest.yml

rest_application:
    type: rest
    resource:     "AppBundle:Rest"
    name_prefix:  api_

的appbundle /控制器/ RestController.php

class RestController extends FOSRestController
{

    public function testrestAction(Request $request)
    {
        $r = [
            'is'    => 'TEST'
        ];
        return $r;
    }

    public function getArticleAction()
    {
        $r = [
            'is'    => 'GET'
        ];
        return $r;
    }

    public function postArticleAction()
    {
        $r = [
            'is'    => 'POST'
        ];
        return $r;
    }
}

我还制作了PUT和DELETE测试方法。所以当我做一些测试电话时

GET / api / testrest

{
    "is": "TEST"
}

GET / api / article

{
    "is": "GET"
}

POST / api / article

No route found for "POST /api/article": Method Not Allowed (Allow: GET, HEAD) (405 Method Not Allowed)

PUT和DELETE也没关系。我错过了一些配置吗?

第二个问题:如果我在Controller文件夹中创建一个API文件夹,我将RestController的命名空间更改为“namespace AppBundle \ Controller \ API;”我将“AppBundle / Resources / config / routing_rest.yml”更新为

resource:     "AppBundle:API:Rest"

然后我收到了这条消息:

Can't locate "AppBundle:API:Rest" controller in /var/www/ws/app/config/routing_rest.yml (which is being imported from "/var/www/ws/app/config/routing.yml").

任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

1选项,运行app/console debug:router(或bin/console debug:router如果v> 2.8),列出生成的路线;

2选项,将RouteResource注释添加到类(例如article),将postArticleAction重命名为postAction并检查POST /api/articles是否响应;

3选项,使用article注释明确添加@POST url,例如。 /** @Post("article") */