James Crinkley
James Crinkley

Reputation: 1408

FOSRestBundle - Having a single resource both standalone and nested as a child of another

I'm working on a Symfony API using FOSRestBundle. I am currently dealing with two entities;

Client Which has a one-to-many relationship to Project.

Both of these entities are working fine as standalone resources (see the following debug:router output)

I've started looking at nesting resources (i.e. having Project as a child resource to Client and have updated the routing as follows;

clients:
  type: rest
  resource: bdd.controller.client

projects:
  type: rest
  parent: clients # newly added
  resource: bdd.controller.project

This works (to a degree) however I now end up with the following routes;

Accessing /clients/1/project calls ProjectController::getProjectAction() (attempting to retrieve the Project with the ID of 1. Is this the intended behaviour now that Project is a child resource? And if so, would Project need to be added as a standalone resource again (duplicating the projects route key and removing parent) to retrieve a single project by ID?

Also, is there a reason that the get_client_projects route has singular-ified client?

Thank you!

Upvotes: 1

Views: 281

Answers (1)

Chris Brown
Chris Brown

Reputation: 4635

The routing issue you're seeing is because your get actions in your ProjectController are missing the parent's argument. Update your getter's method definition to be getProjectsAction($clientId, $id) and the routes should update to what you expect.

Upvotes: 1

Related Questions