如何使用 FOSRestBundle 进行 POST 调用
How to make POST call using FOSRestBundle
我正在尝试使用 FOSRestBunble 设置 RESTFUL 网络服务,但我在进行 POST 调用时遇到了一些问题,这是我的设置:
app/config/routing.yml
rest:
type: rest
resource: "routing_rest.yml"
prefix: /api
app/config/routing_rest.yml
Rest_User:
type: rest
resource: "@AppBundle/Resources/config/routing_rest.yml"
AppBundle/Resources/config/routing_rest.yml
rest_application:
type: rest
resource: "AppBundle:Rest"
name_prefix: api_
AppBundle/Controller/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测试方法。所以当我做一些测试电话时
获取/api/testrest
{
"is": "TEST"
}
获取/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-option, 运行 app/console debug:router
(or bin/console debug:router
if v > 2.8), 列出生成的路由;
2-option,给class添加RouteResource
注解(例如article
),重命名postArticleAction
为postAction
并勾选POST /api/articles
是否有回应;
3-选项,添加 article
url 明确地加上 @POST
注释,例如。 /** @Post("article") */
我正在尝试使用 FOSRestBunble 设置 RESTFUL 网络服务,但我在进行 POST 调用时遇到了一些问题,这是我的设置:
app/config/routing.yml
rest:
type: rest
resource: "routing_rest.yml"
prefix: /api
app/config/routing_rest.yml
Rest_User:
type: rest
resource: "@AppBundle/Resources/config/routing_rest.yml"
AppBundle/Resources/config/routing_rest.yml
rest_application:
type: rest
resource: "AppBundle:Rest"
name_prefix: api_
AppBundle/Controller/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测试方法。所以当我做一些测试电话时
获取/api/testrest
{
"is": "TEST"
}
获取/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-option, 运行 app/console debug:router
(or bin/console debug:router
if v > 2.8), 列出生成的路由;
2-option,给class添加RouteResource
注解(例如article
),重命名postArticleAction
为postAction
并勾选POST /api/articles
是否有回应;
3-选项,添加 article
url 明确地加上 @POST
注释,例如。 /** @Post("article") */