The project is based on Symfony 3.1 + FOSRest 2.0.
I have a controller with following methods:
...
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
delivers the result, but GET /api/categories
leads to 405 Route not found
. Adding slash to the end doesn't solve the issue.
According to names convention cgetAction should deliver a collection of entities by GET /
request. What I'm doing wrong?
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_
I just encountered the same issue. looks like the action declared first in the controller gets overwritten by the second. ie. if cgetCategoryAction is declared first, the getCategoryAction replaces it. so if you swap their order, you'll get the opposite problem. Only one of the routes gets generated.
I resolved it through implicit resource naming
add the implements ClassResourceInterface, and remove the 'Category' from the Action names - FOSRestBundle will work that bit out from the Controller name.
use FOS\RestBundle\Routing\ClassResourceInterface;
class CategoryController extends Controller implements ClassResourceInterface {
public function cgetAction() {...}
public function getAction($id) {...}
}
and you can check what the automatically generated routes look like from the console with the command:
php app/console router:debug