Route and view naming conventions

There are so many ways to maintain routes based on the requirement but i always follow below guidelines which helps me to maintain the file structure and easy to understand.

//listing
Route::get('/teams', '[email protected]');

//Create
Route::get('/teams/create', '[email protected]');

//Store
Route::post('/teams/store', '[email protected]');

//Show
Route::get('/teams/{id}', '[email protected]');

//Edit
Route::get('/teams/{id}/edit', '[email protected]');

//Update
Route::put('/teams/{id}/update', '[email protected]');

//Delete
Route::delete('/teams/{id}/delete', '[email protected]');

for more information related to proper naming convention you may follow below link

https://laravel.com/docs/7.x/controllers#restful-nested-resources


For the get routes, I would normally put the views in a directory structure matching the route name. E.g. resources/views/teams/team/manage/index.blade.php. However, I feel that this is way too verbose.

I agree.


From the Laravel docs:

Laravel uses the typical RESTful "CRUD" approach when assigning resource routes to a controller. Each verb (i.e. GET, POST, PUT, DELETE) gets a designated URI, an action (technically, a controller method) and a route-name (sometimes, /path/to/blade/view).

So, from your snippet:

// return view(teams.index)
Route::get('/teams', '[email protected]');

// return view(teams.create)
Route::get('/teams/create', '[email protected]');

// redirect('/home');
Route::post('/teams', '[email protected]');

// return view('teams.profile')
Route::get('/teams/profile', '[email protected]')->name('profile');

I use this resource table to remind me what-to-do and what-not-do all the time.

Perhaps, inspecting some of the awesome Laravel codebases, might help. Plus, a perspective on how other teams are doing things is always priceless.

I found these to be very helpful:

  • RESTful API Best Practices and Common Pitfalls
  • RESTful API Design - A Cookbook

Update

The key is to stick to the standard CRUD actions i.e. index, show, create, store, edit, update and delete. The views will fall, right into their place.

Check out Adam Wathan's talk at Laracon EU as he demonstrates how, anything can be CRUDDY with a little imagination.