4

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.

SteveEdson
  • 2,485
  • 2
  • 28
  • 46

1 Answers1

0

Yes, you'll have to register every resource. The other alternative would be routes being mapped to controllers automatically based on some naming conventions, but that is not how routes are handled in Laravel.

For cases when a resource requires a small subset of available actions you can use the only option:

Route::resource('example', 'ExampleController', ['only' => ['index']]);

or simply register it as a stand-alone route:

Route::get('example', 'ExampleController@index');
nCrazed
  • 1,025
  • 6
  • 20