1

I have 3 models: User, Lesson and Hotspot. A lesson owns many hotspots, and a user owns many lessons. In the future, I may allow users to have access to other user's lessons but that is a decision I'll make later. So to create a new user, I go to "/user/create". To create a new lesson, I go to "/user/1/create". To create a new hotspot, I go to "/user/1/lesson/1/hotspot/create". When I create a new hotspot, in my hotspot controller I have to ask "does the user own this?" "does this lesson own this?" for every CRUD action, which means my hotspot controller has to know a little about user and lesson, which doesn't really feel right.

Am I doing this correctly, or am I incorrect?

Here are my routes (note: uid == user id, lid == lesson id, hid == hotspot id)

// User CRUD routes
Route::get('/user', 'UserController@index');
Route::get('/user/create', 'UserController@create');
Route::post('/user/create', 'UserController@store');
Route::get('/user/{uid}', 'UserController@show');
Route::get('/user/{uid}/edit', 'UserController@edit');
Route::post('/user/{uid}/edit', 'UserController@update');
Route::post('/user/edit', 'UserController@update');
Route::post('/user/{uid}/delete', 'UserController@destroy');
// Lesson CRUD routes
Route::get('/user/{uid}/lesson/', 'LessonController@index');
Route::get('/user/{uid}/lesson/create', 'LessonController@create');
Route::post('/user/{uid}/lesson/create', 'LessonController@store');
Route::get('/user/{uid}/lesson/{lid}', 'LessonController@show');
Route::get('/user/{uid}/lesson/{lid}/edit', 'LessonController@edit');
Route::post('/user/{uid}/lesson/{lid}/edit', 'LessonController@update');
Route::post('/user/{uid}/lesson/edit', 'LessonController@update');
Route::post('/user/{uid}/lesson/{lid}/delete', 'LessonController@destroy');
// Lesson CRUD routes
Route::get('/user/{uid}/lesson/{lid}/hotspot', 'HotspotController@index');
Route::get('/user/{uid}/lesson/{lid}/hotspot/create', 'HotspotController@create');
Route::post('/user/{uid}/lesson/{lid}/hotspot/create', 'HotspotController@store');
Route::get('/user/{uid}/lesson/{lid}/hotspot/{hid}', 'HotspotController@show');
Route::get('/user/{uid}/lesson/{lid}/hotspot/{hid}/edit', 'HotspotController@edit');
Route::post('/user/{uid}/lesson/{lid}/hotspot/{hid}/edit', 'HotspotController@update');
Route::post('/user/{uid}/lesson/{lid}/hotspot/{hid}/edit', 'HotspotController@update');
Route::post('/user/{uid}/lesson/{lid}/hotspot/{hid}/delete', 'HotspotController@destroy');
Laurence
  • 58,936
  • 21
  • 171
  • 212
Andrew Allbright
  • 3,721
  • 3
  • 20
  • 23

2 Answers2

2

Assuming you're building some kind of an API.

With this in mind you don't really need /create, /edit and /delete parts anywhere: all of this is solved by using appropriate HTTP methods.

Community
  • 1
  • 1
Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
  • So what you're saying is I should use the URL schema I'm suggesting but I need to readjust the HTTP verb words? – Andrew Allbright Apr 26 '14 at 14:03
  • 1
    @AndrewAllbright If what you're building is a REST API, you don't really need a `GET /user/{uid}/edit` route. Rather, you need a `POST /users/` to create, `DELETE /users/{uid}` to delete, etc. – Anton Gogolev Apr 26 '14 at 14:05
  • I can fix my REST Api... What about the url schema? Is it good? Edit: here is the fixed rest API // User CRUD fixed Route::post('/user/'); // create Route::get('/user/{uid}'); // read Route::put('/user/{uid}'); // update Route::delete('/user/{uid}');// delete – Andrew Allbright Apr 26 '14 at 14:08
2

Use a nester resource.

Route::resource('user.lesson', LessonController');

You can go as deep as you like.

Lee
  • 20,034
  • 23
  • 75
  • 102
  • [Looking into this now](http://laravel.com/docs/controllers#resource-controllers). I may have questions- will post back if I do. This looks like it may be what I'm looking for- will accept as answer if it is. – Andrew Allbright Apr 26 '14 at 14:27
  • DUDE! It's BEAUTIFUL! Route::resource('user', 'UserController'); Route::resource('user.lesson', 'LessonController'); Route::resource('user.lesson.hotspot', 'HotspotController'); – Andrew Allbright Apr 26 '14 at 14:54
  • Very powerful hey :-) – Lee Apr 26 '14 at 16:26