📛
Laravelの命名で困った時に見る
LaravelでWeb開発楽しんでますか?
Laravelは自由度が高い一方で、逆に自由すぎて秩序が守りにくいなーなんてことあると思います。
そんな時は Laravel Best Practices を参考にしましょう。
まんまコピペですが以下のようになっています。
| What | How | Good | Bad |
|---|---|---|---|
| Controller | singular | ArticleController | |
| Route | plural | articles/1 | |
| Route name | snake_case with dot notation | users.show_active | |
| Model | singular | User | |
| hasOne or belongsTo relationship | singular | articleComment | |
| All other relationships | plural | articleComments | |
| Table | plural | article_comments | |
| Pivot table | singular model names in alphabetical order | article_user | |
| Table column | snake_case without model name | meta_title | |
| Model property | snake_case | $model->created_at | |
| Foreign key | singular model name with _id suffix | article_id | |
| Primary key | - | id | |
| Migration | - | 2017_01_01_000000_create_articles_table | |
| Method | camelCase | getAll | |
| Method in resource controller | table | store | |
| Method in test class | camelCase | testGuestCannotSeeArticle | |
| Variable | camelCase | $articlesWithAuthor | |
| Collection | descriptive, plural | $activeUsers = User::active()->get() | |
| Object | descriptive, singular | $activeUser = User::active()->first() | |
| Config and language files index | snake_case | articles_enabled | |
| View | kebab-case | show-filtered.blade.php | |
| Config | snake_case | google_calendar.php | |
| Contract (interface) | adjective or noun | AuthenticationInterface | |
| Trait | adjective | Notifiable | |
| Trait (PSR) | adjective | NotifiableTrait | |
| Enum | singular | UserType |
|
| FormRequest | singular | UpdateUserRequest |
|
| Seeder | singular | UserSeeder |
それ以外
| What | How | Good | Bad | Resource |
|---|---|---|---|---|
| Neted Controller | plural | Posts/ArticleController | Laravel 5.4 Controllers & Namespaces |
Discussion