我正在尝试使用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").
任何帮助表示赞赏
答案 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") */