Question

I have an issue with FOSRestBundle.

I am trying to use the parent option in my routing and 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

Here is my routing.yml

organisation : 
  type: rest
  resource: MyProject\RestBundle\Controller\OrganisationRestController
  name_prefix:  api_  
users : 
  type: rest
  resource: MyProject\RestBundle\Controller\UsersRestController
  name_prefix:  api_
  parent: organisation

I have implemented the ClassRessourceInterface in my controllers.

Why do I get the above error?

Was it helpful?

Solution

You are trying to implement a Resource Collection here.

As the documentation says:

In this case, your UsersController MUST always have a single resource get... action

class UsersController extends Controller
{
    public function getUserAction($slug)
    {} // "get_user"   [GET] /users/{slug}

    ...
}

But the difference to the example is that your Organisation is the parent and NOT the User.

your MyProject\RestBundle\Controller\IrganizationsRestController has to provide something like a getOrganizationsRestAction(). OrganisationsRest ... is your parent.

I'm not sure what name FOSRestBundle expects when trying to access the singular form of UsersRest ...

As your Controller names are not providing any extra information being suffixed with "Rest" because your bundle is already named Rest Bundle ... you might consider changing the controller names to:

 OrganisationsController
 UsersController

and add the getOrganisationAction() method to the OrganisationsController as in the example.

OTHER TIPS

Maybe you are forgetting add, in the nested Entity, the nested ID parameter in this get{SINGULAR}Action($id):

class SubEntityController {
    ...
    getSubEntityAction($subId, $id) {
       ...   
    }
}

... getSubSub....($subSubId, $subId, $id) ... an so on, on each nested entity.

This one Works for Me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top