Naming Conventions - MrPunyapal/go-get-laravel GitHub Wiki

Here are some naming conventions for Laravel you should follow.

Table of Contents

Naming Conventions for Laravel

Relationships

  • All other relationships: plural, camelCase

[!TIP] Good Examples:

  • articles, roles, permissions, articles, comments, photos, childComments

[!WARNING] Bad Examples:

  • getArticles, article, child_comments, ChildComments
  • hasOne or belongsTo relationship: singular, camelCase

[!TIP] Good Examples:

  • article, comment, photo, childComment

[!WARNING] Bad Examples:

  • getArticle, articles, child_comments, ChildComment

Config and Language Files

  • Config: snake_case

[!TIP] Good Examples:

  • articles_enabled, roles_enabled, permissions_enabled

[!WARNING] Bad Examples:

  • ArticlesEnabled, roles-enabled, permissionsEnabled
  • Config and language files index: snake_case

[!TIP] Good Examples:

  • articles_enabled, roles_enabled, permissions_enabled

[!WARNING] Bad Examples:

  • ArticlesEnabled, roles-enabled, permissionsEnabled

Controllers

  • Controller: singular

[!TIP] Good Examples:

  • ArticleController, UserController, RoleController, PermissionController

[!WARNING] Bad Examples:

  • ArticlesController, Users, Roles, Permissions
  • Method: camelCase or 7 restful methods

[!TIP] Good Examples:

  • index, store, create, show, update, destroy, edit

[!WARNING] Bad Examples:

  • getAll, createNew, storeArticle, showArticle, updateArticle, deleteArticle, editArticle

Contracts (Interfaces)

  • Contract (interface): adjective or noun

[!TIP] Good Examples:

  • Authenticatable, Authorizable, CanResetPassword, MustVerifyEmail

[!WARNING] Bad Examples:

  • Authentication, Authorization, ResetPassword, VerifyEmail

Enums

  • Enum (interface): singular

[!TIP] Good Example:

  • UserType

[!WARNING] Bad Examples:

  • UserTypes, UserTypeEnum

Database

  • Foreign key (interface): singular model name with _id suffix

[!TIP] Good Example:

  • article_id

[!WARNING] Bad Examples:

  • ArticleId, id_article, articles_id
  • Migration (interface): YYYY_MM_DD_HHMMSS_create_table_name

[!TIP] Good Example:

  • 2017_01_01_000000_create_articles_table

[!WARNING] Bad Example:

  • 2017_01_01_000000_articles
  • Model (interface): singular

[!TIP] Good Example:

  • User

[!WARNING] Bad Example:

  • Users
  • Pivot table (interface): singular model names in alphabetical order

[!TIP] Good Example:

  • article_user

[!WARNING] Bad Examples:

  • user_article, articles_users
  • Primary key (interface): id

[!TIP] Good Example:

  • id

[!WARNING] Bad Example:

  • custom_id
  • Table (interface): plural

[!TIP] Good Example:

  • article_comments

[!WARNING] Bad Examples:

  • article_comment, articleComments
  • Table column (interface): snake_case without model name

[!TIP] Good Example:

  • meta_title

[!WARNING] Bad Examples:

  • MetaTitle; article_meta_title

Requests and Forms

  • FormRequest (interface): singular

[!TIP] Good Example:

  • UpdateUserRequest

[!WARNING] Bad Examples:

  • UpdateUserFormRequest, UserFormRequest, UserRequest
  • Method in resource controller (interface): table

[!TIP] Good Example:

  • store

[!WARNING] Bad Example:

  • saveArticle

Methods

  • Method (interface): camelCase

[!TIP] Good Example:

  • get_all

[!WARNING] Bad Example:

  • getAll
  • Method in test class (interface): camelCase

[!TIP] Good Example:

  • test_guest_cannot_see_article

[!WARNING] Bad Example:

  • testGuestCannotSeeArticle

Objects and Variables

  • Object (interface): descriptive, singular

[!TIP] Good Example:

  • $active_user = User::active()->first()

[!WARNING] Bad Examples:

  • $users, $obj
  • Variable (interface): camelCase

[!TIP] Good Example:

  • $articles_with_author

[!WARNING] Bad Example:

  • $articlesWithAuthor

Routes

  • Route (interface): plural

[!TIP] Good Example:

  • articles/1

[!WARNING] Bad Example:

  • article/1
  • Route name (interface): snake_case with dot notation

[!TIP] Good Example:

  • users.show_active

[!WARNING] Bad Examples:

  • users.show-active, show-active-users

Views

  • View (interface): kebab-case

[!TIP] Good Example:

  • show-filtered.blade.php

[!WARNING] Bad Examples:

  • showFiltered.blade.php, show_filtered.blade.php

Tests

  • Test class (interface): singular, descriptive

[!TIP] Good Example:

  • ArticleControllerTest

[!WARNING] Bad Example:

  • ArticlesControllerTest
  • Test method (interface): snake_case, descriptive

[!TIP] Good Example:

  • test_guest_cannot_see_article

[!WARNING] Bad Example:

  • testGuestCannotSeeArticle

Constants

  • Constant (interface): uppercase, snake_case

[!TIP] Good Example:

  • MAX_ATTEMPTS

[!WARNING] Bad Example:

  • maxAttempts

Helper Functions

  • Function (interface): camelCase

[!TIP] Good Example:

  • generateToken

[!WARNING] Bad Example:

  • generate_token

Middleware

  • Middleware (interface): singular, descriptive

[!TIP] Good Example:

  • AuthenticateUser

[!WARNING] Bad Example:

  • AuthenticateUsers

Traits

  • Trait (interface): adjective or noun

[!TIP] Good Example:

  • HasPermissions

[!WARNING] Bad Examples:

  • HasPermission, PermissionTrait

Exceptions

  • Exception (interface): singular, descriptive

[!TIP] Good Example:

  • InvalidCredentialsException

[!WARNING] Bad Example:

  • InvalidCredentialException

Events

  • Event (interface): past tense, descriptive

[!TIP] Good Example:

  • UserLoggedIn

[!WARNING] Bad Example:

  • UserLogin

Jobs

  • Job (interface): descriptive, present tense

[!TIP] Good Example:

  • SendEmail

[!WARNING] Bad Example:

  • EmailSender

Queues

  • Queue (interface): descriptive, present tense

[!TIP] Good Example:

  • ProcessPayment

[!WARNING] Bad Example:

  • PaymentProcessor

Policies

  • Policy (interface): singular, descriptive

[!TIP] Good Example:

  • ArticlePolicy

[!WARNING] Bad Example:

  • ArticlesPolicy

Factories

  • Factory (interface): singular, descriptive

[!TIP] Good Example:

  • UserFactory

[!WARNING] Bad Example:

  • UsersFactory

Seeders

  • Seeder (interface): plural, descriptive

[!TIP] Good Example:

  • UsersTableSeeder

[!WARNING] Bad Example:

  • UserTableSeeder

Resource Table

Verb URI Action Route Name
GET /photos index photos.index
GET /photos/create create photos.create
POST /photos store photos.store
GET /photos/{photo} show photos.show
GET /photos/{photo}/edit edit photos.edit
PUT/PATCH /photos/{photo} update photos.update
DELETE /photos/{photo} destroy photos.destroy