2016-07-26 57 views
2

我想安装使用FOSRestBunble RESTful Web服务进行POST调用,但我有一些问题进行POST调用,这里是我的设置:如何使用FOSRestBundle

应用程序/配置/ 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 /条

{ 
    "is": "GET" 
} 

POST/API /条

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 /资源/配置/ 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"). 

任何帮助赞赏

回答

0

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

2-选项,添加RouteResource批注类,重命名postArticleActionpostAction并检查POST /api/articles正在响应或不(例如article);

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

+0

我解决了路由器的调试问题,基本上是自动生成的路由为postArticleAction方法创建/ api/articles。 但是,如果添加一个方法postArticlesAction,我会有2个路径(api_post_article和api_post_articles)都指向/ api /文章,这对我来说没有任何意义,也许是一个错误?无论如何感谢您的帮助,并感谢提醒我使用命令行进行调试。 – user3174311