Each parent controller must have a `get {SINGULAR} action ($ id)` when I have a multi-level auxiliary resource in the FOS Rest Bundle

I have three controllers named BlogController , PostController , CommentController , which CommentController is a helper resource of the PostController resource and PostController resource.

 /** * @Rest\RouteResource("blog", pluralize=false) */ class BlogController extends FOSRestController { public function getAction($blogUri) { ... } } /** * @Rest\RouteResource("post", pluralize=false) */ class PostController extends FOSRestController { public function getAction($postId) { ... } } /** * @Rest\RouteResource("comment", pluralize=false) */ class CommentController extends FOSRestController { public function getAction($commentId) { ... } } 

routing.yml

 mgh_blog: resource: MGH\BlogBundle\Controller\BlogController type: rest mgh_blog_post: resource: MGH\BlogBundle\Controller\PostController type: rest parent: mgh_blog mgh_blog_post_comment: resource: MGH\PostBundle\Controller\CommentController type: rest parent: mgh_blog_post 

I define getAction methods, but I get the following error:

 [InvalidArgumentException] Every parent controller must have `get{SINGULAR}Action($id)` method where {SINGULAR} is a singular form of associated object 

Edit:

I am also trying to change the method name to getCommentAction($commentId) , getPostAction($postId) and getBlogAction , but this does not work.

When I use @RouteResource annotations, the method name should be getAction($id) , otherwise it does not work.

When I change the parent address of mgh_blog_post_comment router to mgh_blog , it works!

+7
symfony fosrestbundle
source share
3 answers

This error description is awful and a long time because it does not tell you what the real problem is. Try the following:

 /** * @Rest\RouteResource("blog", pluralize=false) */ class BlogController extends FOSRestController { public function getAction($blogUri) { ... } } /** * @Rest\RouteResource("post", pluralize=false) */ class PostController extends FOSRestController { public function getAction($blogUri, $postId) { ... } } /** * @Rest\RouteResource("comment", pluralize=false) */ class CommentController extends FOSRestController { public function getAction($blogUri, $postId, $commentId) { ... } } 

You did not have the correct number of arguments in the actions of the child controller. It took me two days of debugging to figure this out.

The parent route, Blog, looks like this:

 /blog/{blogUri} 

It will match

 public function getAction($blogUri) 

Children's route, message, looks like this:

 /blog/{blogUri}/post/{postId} 

It will not match the code below because it needs two parameters. The same is true for a grandson - who is looking for three parameters:

 public function getAction($postId) 

The grandson's path, a comment, looks like this:

 /blog/{blogUri}/post/{postId}/comment/{commentId} 

The code tracks the ancestors of each controller. The post has 1 ancestor. When building routes for the post controller, the code looks at the number of parameters in the "get action". It takes the number of parameters and subtracts the number of ancestors. If the difference is not equal to one, it throws an error.

The conclusion for each descendant should include the identification parameters of its ancestors and its own identifier. There should always be one more parameter than the ancestors.

+5
source share

You tried?:

 class CommentController extends FOSRestController { public function getCommentAction($commentId) { ... } } 
+1
source share

Try:

 public function cgetAction(Request $request) { ... } 

This is my example controller:

 <?php namespace Cf\SClinicBundle\Controller; use Symfony\Component\HttpFoundation\Request; use FOS\RestBundle\Controller\FOSRestController; use FOS\RestBundle\Controller\Annotations as Rest; use FOS\RestBundle\Controller\Annotations\RouteResource; use Cf\SClinicBundle\Entity\CfAcquireImage; use Doctrine\DBAL\DBALException as DBALException; use Doctrine\ORM\NoResultException as NoResultException; /** * CfAcquireImage controller. * * @RouteResource("acquire-image") */ class ApiCfAcquireImageController extends FOSRestController { /** * @var array */ public $status; /** * @var */ public $parameter; /** * @var */ private $role_name; /** * Constructor */ public function __construct() { } /** * Lists all Cf Acquire Image entities. * * @param Request $request * * @return mixed */ public function cgetAction(Request $request) { } /** * Finds a Cf Acquire Image entity by id. * * @param Request $request * @param $id $id * * @return array */ public function getAction(Request $request, $id) { } /** * Create a new Cf Acquire Image entity. * * @param Request $request * * @return mixed */ public function postAction(Request $request) { } /** * @param Request $request * @param $id * * @return array */ public function putAction(Request $request, $id) { } /** * Deletes a Cf Acquire Image entity. * * @param Request $request * @param $id * * @return mixed */ public function deleteAction(Request $request, $id) { } } 
+1
source share

All Articles