laravel 路由和视图命名约定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41415148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:02:12  来源:igfitidea点击:

Route and view naming conventions

laravelnaming-conventions

提问by AshMenhennett

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 getroutes, 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.

对于get路由,我通常会将视图放在与路由名称匹配的目录结构中。例如resources/views/teams/team/manage/index.blade.php。但是,我觉得这太冗长了。

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 teamis not used, so when I have other views, like so (using the original examples convention): resources/views/teams/choose.indexthey dont visually have the relationship intended. I.e. they have a differing 'root' directory- teamsvs team.

我觉得如果我使用像这样的视图目录结构而不是最后一个例子,它会全面混淆(对我自己和其他开发人员):resources/views/team/manage/index.blade.php-team不使用复数,所以当我有其他视图时,比如所以(使用原始示例约定):resources/views/teams/choose.index它们在视觉上没有预期的关系。即他们有一个不同的“根”目录 -teamsteam.

Any input or advice would be appreciated.

任何意见或建议将不胜感激。

回答by joshuamabina

For the getroutes, 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.

对于get路由,我通常会将视图放在与路由名称匹配的目录结构中。例如resources/views/teams/team/manage/index.blade.php。但是,我觉得这太冗长了。

I agree.

我同意。



From the Laravel docs:

来自Laravel 文档

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).

Laravel 在将资源路由分配给控制器时使用典型的 RESTful“CRUD”方法。每个动词(即 GET、POST、PUT、DELETE)都获得一个指定的URI、一个动作(技术上,一个控制器方法)和一个路由名称(有时,/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 tableto 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.

也许,检查一些很棒的 Laravel 代码库可能会有所帮助。另外,对其他团队如何做事的看法总是无价的。

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.

关键是坚持标准的 CRUD 操作,即索引、显示、创建、存储、编辑、更新和删除。意见将落入他们的位置。

Check out Adam Wathan's talk at Laracon EUas he demonstrates how, anything can be CRUDDYwith a little imagination.

看看Adam Wathan 在 Laracon EU 上的演讲,他演示了如何通过一点点想象力将任何事物变得 CRUDDY

回答by Soubhagya Kumar Barik

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

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