找不到 Symfony3 FOSRest cgetAction 路由
Symfony3 FOSRest cgetAction route not found
项目基于Symfony 3.1 + FOSRest 2.0.
我有一个具有以下方法的控制器:
...
public function cgetCategoryAction()
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('AppBundle:Category')->findAll();
if (!$entity) {
return [];
}
return $entity;
}
public function getCategoryAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('AppBundle:Category')->find($id);
if (!$entity) {
throw new NotFoundHttpException(sprintf('The resource \'%s\' was not found.', $id));
}
return $entity;
}
...
GET /api/categories/1
提供结果,但 GET /api/categories
导致 405 Route not found
。在末尾添加斜线并不能解决问题。
根据名称约定,cgetAction 应通过 GET /
请求传送实体集合。我做错了什么?
更新
routing.yml
:
app:
type: rest
prefix: /api
resource: "@AppBundle/Resources/config/api-routing.yml"
NelmioApiDocBundle:
resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
prefix: /api/doc
routing-api.yml
:
api_Category:
type: rest
resource: "@AppBundle/Controller/CategoryController.php"
name_prefix: api_
api_Product:
type: rest
resource: "@AppBundle/Controller/ProductController.php"
name_prefix: api_
顺便说一下,在您的代码中:
if (!$entity) {
return [];
}
这不会按您预期的方式工作。 $entity 是一个结果集。所以你需要检查计数:
if ( count($entity) == 0 ) {
return [];
}
以上检查是否为空
我刚遇到同样的问题。看起来控制器中首先声明的操作被第二个覆盖。 IE。如果先声明 cgetCategoryAction,则 getCategoryAction 会替换它。所以如果你交换他们的订单,你会遇到相反的问题。仅生成一条路线。
添加实现 ClassResourceInterface,并从 Action 名称中删除 'Category' - FOSRestBundle 将从 Controller 名称中删除。
use FOS\RestBundle\Routing\ClassResourceInterface;
class CategoryController extends Controller implements ClassResourceInterface {
public function cgetAction() {...}
public function getAction($id) {...}
}
您可以使用以下命令从控制台检查自动生成的路由是什么样的:
php app/console router:debug
项目基于Symfony 3.1 + FOSRest 2.0.
我有一个具有以下方法的控制器:
...
public function cgetCategoryAction()
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('AppBundle:Category')->findAll();
if (!$entity) {
return [];
}
return $entity;
}
public function getCategoryAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('AppBundle:Category')->find($id);
if (!$entity) {
throw new NotFoundHttpException(sprintf('The resource \'%s\' was not found.', $id));
}
return $entity;
}
...
GET /api/categories/1
提供结果,但 GET /api/categories
导致 405 Route not found
。在末尾添加斜线并不能解决问题。
根据名称约定,cgetAction 应通过 GET /
请求传送实体集合。我做错了什么?
更新
routing.yml
:
app:
type: rest
prefix: /api
resource: "@AppBundle/Resources/config/api-routing.yml"
NelmioApiDocBundle:
resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
prefix: /api/doc
routing-api.yml
:
api_Category:
type: rest
resource: "@AppBundle/Controller/CategoryController.php"
name_prefix: api_
api_Product:
type: rest
resource: "@AppBundle/Controller/ProductController.php"
name_prefix: api_
顺便说一下,在您的代码中:
if (!$entity) {
return [];
}
这不会按您预期的方式工作。 $entity 是一个结果集。所以你需要检查计数:
if ( count($entity) == 0 ) {
return [];
}
以上检查是否为空
我刚遇到同样的问题。看起来控制器中首先声明的操作被第二个覆盖。 IE。如果先声明 cgetCategoryAction,则 getCategoryAction 会替换它。所以如果你交换他们的订单,你会遇到相反的问题。仅生成一条路线。
添加实现 ClassResourceInterface,并从 Action 名称中删除 'Category' - FOSRestBundle 将从 Controller 名称中删除。
use FOS\RestBundle\Routing\ClassResourceInterface;
class CategoryController extends Controller implements ClassResourceInterface {
public function cgetAction() {...}
public function getAction($id) {...}
}
您可以使用以下命令从控制台检查自动生成的路由是什么样的:
php app/console router:debug