I'm using Laravel 5.2 to create a REST API, and I'm trying to follow the best practices for API design, documented here. One of the points mentions minimising path nesting, like so:
In data models with nested parent/child resource relationships, paths may become deeply nested, e.g.:
/orgs/{org_id}/apps/{app_id}/dynos/{dyno_id}
Limit nesting depth by preferring to locate resources at the root path. Use nesting to indicate scoped collections. For example, for the case above where a dyno belongs to an app belongs to an org:
/orgs/{org_id} /orgs/{org_id}/apps /apps/{app_id} /apps/{app_id}/dynos /dynos/{dyno_id}
From https://github.com/interagent/http-api-design/blob/master/requests/minimize-path-nesting.md
What is the best way to do this using Laravel's controllers and routes? Currently I am using:
Route::resource('orgs', 'OrganisationController', ['except' => ['edit', 'create']]);
Route::resource('apps', 'AppController', ['except' => ['edit', 'create']]);
Currently, I'm thinking I need to add another single route for orgs/{org_id}/apps/
, which uses only the AppController@index
method. These are only examples, I have a lot of resources so the above code would have to be repeated for each one.
Is this the best way of doing things, or is there a cleaner alternative that i'm not aware of?
Thanks.