14

I am looking for some input regarding the naming conventions I use for route names and view directory structures.

Say I have the following routes:

Route::get('/teams/choose', 'ChooseTeamController@index')->name('teams.choose.index'); Route::post('/teams/choose', 'ChooseTeamController@choose')->name('teams.choose'); Route::get('/teams/{team}/manage', 'ManageTeamController@index')->name('teams.team.manage.index'); 

For the get routes, I would nornally 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 feel that it would be confusing all round (to myself and other developers) if I was to use a view directory structure like so, rather than the last example: resources/views/team/manage/index.blade.php- the plural of team is not used, so when I have other views, like so (using the original examples convention): resources/views/teams/choose.index they dont visually have the relationship intended. I.e. they have a differing 'root' directory- teams vs team.

Any input or advice would be appreciated.

3 Answers 3

26

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', 'TeamController@index'); // return view(teams.create) Route::get('/teams/create', 'TeamsController@create'); // redirect('/home'); Route::post('/teams', 'TeamController@store'); // return view('teams.profile') Route::get('/teams/profile', 'TeamController@profile')->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:


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.

Sign up to request clarification or add additional context in comments.

Comments

4

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', 'TeamController@index'); //Create Route::get('/teams/create', 'TeamController@create'); //Store Route::post('/teams/store', 'TeamController@store'); //Show Route::get('/teams/{id}', 'TeamController@show'); //Edit Route::get('/teams/{id}/edit', 'TeamController@edit'); //Update Route::put('/teams/{id}/update', 'TeamController@update'); //Delete Route::delete('/teams/{id}/delete', 'TeamController@delete'); 

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

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

1 Comment

Do not use verbs in routes, instead of Route::delete('/teams/{id}/delete', 'TeamController@delete'); use Route::delete('/teams/{id}', 'TeamController@delete');
0

if you are building with consuming by api as a concern, you wouldn't need the create and edit forms so endpoints can be reduced to:

//listing Route::get('/teams', 'TeamController@index'); //Store Route::post('/teams', 'TeamController@store'); //Show Route::get('/teams/{id}', 'TeamController@show'); //Update Route::put('/teams/{id}', 'TeamController@update'); //Delete Route::delete('/teams/{id}', 'TeamController@delete'); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.