Naming Conventions
Table of Contents
Follow Laravel naming conventions
Follow PSR standards.
Also, follow naming conventions accepted by Laravel community:
What | How | Good | Bad |
---|---|---|---|
Controller | singular | ArticleController | ~~ArticlesController~~ |
Route | plural | articles/1 | ~~article/1~~ |
Named route | snake_case with dot notation | users.show_active | ~~users.show-active, show-active-users~~ |
Model | singular | User | ~~Users~~ |
hasOne or belongsTo relationship | singular | articleComment | ~~articleComments, article_comment~~ |
All other relationships | plural | articleComments | ~~articleComment, article_comments~~ |
Table | plural | article_comments | ~~article_comment, articleComments~~ |
Pivot table | singular model names in alphabetical order | article_user | ~~user_article, articles_users~~ |
Table column | snake_case without model name | meta_title | ~~MetaTitle; article_meta_title~~ |
Model property | snake_case | $model->created_at | ~~$model->createdAt~~ |
Foreign key | singular model name with _id suffix | article_id | ~~ArticleId, id_article, articles_id~~ |
Primary key | - | id | ~~custom_id~~ |
Migration | - | 2017_01_01_000000_create_articles_table | ~~2017_01_01_000000_articles~~ |
Method | camelCase | getAll | ~~get_all~~ |
Method in resource controller | table | store | ~~saveArticle~~ |
Method in test class | camelCase | testGuestCannotSeeArticle | ~~test_guest_cannot_see_article~~ |
Variable | camelCase | $articlesWithAuthor | ~~$articles_with_author~~ |
Collection | descriptive, plural | $activeUsers = User::active()->get() | ~~$active, $data~~ |
Object | descriptive, singular | $activeUser = User::active()->first() | ~~$users, $obj~~ |
Config and language files index | snake_case | articles_enabled | ~~ArticlesEnabled; articles-enabled~~ |
View | snake_case | show_filtered.blade.php | ~~showFiltered.blade.php, show-filtered.blade.php~~ |
Config | snake_case | google_calendar.php | ~~googleCalendar.php, google-calendar.php~~ |
Contract (interface) | adjective or noun | Authenticatable | ~~AuthenticationInterface, IAuthentication~~ |
Trait | adjective | Notifiable | ~~NotificationTrait~~ |
Controllers
All controllers MUST start with a singular noun followed by the word "Controller" at the end, and MUST be in PascalCase.
Do
class PostController extends Controller
Don't
class Posts_Controller extends Controller
class post_controller extends Controller
class Posts extends Controller
Models
All models MUST be in singular form and MUST use PascalCasing.
Do
class User extends Models
Don't
class Users extends Models
class UserModel extends Models
class user_model extends Models
Model Properties
All model properties SHOULD be in snake_case.
Do
public $first_name
public $published_at
Don't
public $firstName
public $publishedAt
Relationships: hasOne and belongsTo
All methods defining hasOne or belongsTo relationship MUST be in singular form.
Do
// ...
public function post()
{
return $this->hasOne(App\Models\Post::class);
}
// ...
Don't
// ...
public function posts()
{
return $this->hasOne(App\Models\Post::class);
}
// ...
Relationships: hasMany, belongsToMany
All methods defining hasMany or belongsToMany relationship MUST be in plural form.
Do
// ...
public function users()
{
return $this->hasMany(App\Models\Post::class);
}
// ...
Don't
// ...
public function user()
{
return $this->hasMany(App\Models\Post::class);
}
// ...
Controllers/Models Methods
All methods in Controllers and Models MUST be in camelCase.
Do
// ...
public function scopeActive($q)
{
return $q->where('active', 1);
}
// ...
Don't
// ...
public function scope_active($q)
{
}
// ....
public function ScopeActive($q)
{
}
// ...
Variables
All variables SHOULD be in camelCase.
Do
$popularPosts
Don't
$popular_posts
$PopularPosts
Avoid abbreviations as much as possible.
Do
$invoiceAccountId
Don't
$invAccId
Routes
All routes SHOULD be in plural form of the resource it's manipulating, and SHOULD be in lower case. They MUST use kebab-casing.
Do
Route::get('/users', 'UserController@index');
Route::get('/user-policies', 'UserPolicyController@index');
Don't
Route::get('/user', 'UserController@index');
Route::get('/user_all', 'UserController@index');
Route::get('/user_policies', 'UserController@index');
Route Names
All route names MUST use snake_casing and dot notation. They SHOULD be in singular form.
Do
Route::get('/users/active', 'UserController@index')->name('user.active');
Route::get('/users/user-policies', 'UserPolicyController@index')->name('user.user_policy');
Don't
Route::get('/users/active', 'UserController@index')->name('users_active');
Route::get('/users/user-policies', 'UserPolicyController@index')->name('user.user_policies');
Route::get('/users/user-policies', 'UserPolicyController@index')->name('users-user-policies');
Views
All view files / folders MUST use snake_casing.
Do
user_policies/show.blade.php
user_policies.blade.php
Don't
user-policies/show.blade.php
UserPolicies/show.blade.php
userPolicies.blade.php
Parameters
All view parameters SHOULD BE in camelCase.
Do
return view('index.blade.php' [
'firstName' => 'Taylor',
'lastName' => 'Otwell'
]);
Don't
return view('index.blade.php' [
'first_name' => 'Taylor',
'last-name' => 'Otwell'
]);
Database
Tables
Database tables SHOULD be in plural form and SHOULD use snake_casing.
Do
users
user_policies
Don't
user
userPolicies
Pivot tables SHOULD be in singular form and in alphabetical order.
Do
post_user
Don't
user_post
posts_users
Columns
All columns SHOULD be in snake_case and SHOULD NOT contain model name.
Do
first_name
address
country
Don't
firstName
user_address
user_country
Primary id
column in the table SHOULD be named just 'id' in lower case and SHOULD NOT contain any prefix or a suffix.
Do
id
Don't
post_id
postID
Foreign key columns SHOULD use singular model name with '_id' suffix.
Do
post_id
Don't
postId
fk_post_id
Comments (0)
What are your thoughts on "Naming Conventions"?
You need to create an account to comment on this post.