Hey guys, In this article, I want to describe the Laravel [PHP] coding Standards with PSR.
To make this tutorial more straightforward, I will divide the full tutorial into several articles. So this is the first article.
Article 1: Naming Conventions. ✊
Here we will talk about naming conventions about PHP. Following conventions accepted by the Laravel community.
01.01 Controller 👈
- Name should be in singular form.
- Should use PascalCase.
//Should Do
"CustomerController.php"
//Shouldn't Do
"CustomersController.php"
01.02 Route 👈
01.02.01 Route Url 👈
- Url should be in plural form.
- Can use kebab-case if there are two words in a single part For Best Practice.
//Should Do
"https://devdojo.com/customers/25"
"https://devdojo.com/customers/password-reset"
//Shouldn't Do
"https://devdojo.com/customer/25"
"https://devdojo.com/customers/passwordReset"
01.02.02 Route Name 👈
- Should use snake_case with dot notation.
- Better to use the same name as in the URL.
//Should Do
->('customers.view');
->('customers.password_reset');
//Should't Do
->('customers-view');
->('customers_view');
->('customers.password.reset');
->('customers.password-reset');
->('customer-password-reset');
01.03 DataBase Related 👈
01.03.01 Migration 👈
- Should use the name as what you want to do with snake_case.
//Should Do
"2021_03_19_033513_create_customers_table.php"
"2021_03_19_033513_add_image_id_to_customers_table.php"
"2021_03_19_033513_drop_image_id_from_customers_table.php"
//Shouldn't Do
"2021_03_19_033513_customers.php"
"2021_03_19_033513_add_image_id_customers.php"
"2021_03_19_033513_remove_image_id_customers.php"
01.03.02 Table 👈
- Table name must be in plural form.
- Should use snake_case.
//Should Do
"customers","cart_items"
//Shouldn't Do
"customer" ,"cartItems","CartItems","Cart_item"
01.03.03 Pivot Table 👈
- Table name must be in singular form.
- Should use snake_case
- Names should be in alphabetical Order.
//Should Do
"course_student"
//Shouldn't Do
"student_courses","students_courses","course_students",
01.03.04 Table Columns 👈
- Should use snake_case.
- Should not use table name with column names.
- Readable name can be used for better practice.
//Should Do
"first_name"
//Shouldn't Do
"user_first_name","FirstName"
01.03.05 Foreign key 👈
- Should use snake_case.
- Should use singular table name with id prefix.
//Should Do
"course_id"
//Shouldn't Do
"courseId","id","courses_id","id_course"
01.03.06 Primary key 👈
- only use the name as the id.
//Should Do
"id"
//Shouldn't Do
"custom_name_id"
01.03.07 Model 👈
- Model name must be in singular form.
- Should Use PascalCase
- Model name must be a singular form or table name.
//Should Do
"Customer"
//Shouldn't Do
"Customers" ,"customer"
01.03.08 Model Single relations [Has One, Belongs To] 👈
- Method name must be in singular form.
- Should Use camalCase
//Should Do
"studentCourse"
//Shouldn't Do
"StudentCourse" ,"student_course" ,"studentCourses"
01.03.09 Model all other relations and methods [Has Many, other] 👈
- Method name must be in plural form.
- Should use camalCase
//Should Do
"cartItems"
//Shouldn't Do
"CartItem" ,"cart_item" ,"cartItem"
01.04 Functions 👈
- Should Use snake_case
//Should Do
"show_route"
//Shouldn't Do
"showRoute" ,"ShowRoute"
01.05 Methods in resources controller 👈
- Should use camelCase
- Must use singles words related to action
//Should Do
"store"
//Shouldn't Do
"saveCustomer"
//Should Do
"show"
//Shouldn't Do
"viewCustomer"
//Should Do
"destroy"
//Shouldn't Do
"deleteCustomer"
//Should Do
"index"
//Shouldn't Do
"allCustomersPage"
01.06 Variables 👈
- Should use camelCase
- Must use readable words which describe the value.
//Should Do
$customerMessages;
//Should't Do
$CustomerMessages;
$customer_messages;
$c_messages;
$c_m;
01.07 Collection 👈
- Must describe the value.
- Must be plural
//Should Do
$verifiedCustomers = $customer->verified()->get();
//Should't Do
$verified;
$data;
$resp;
$v_c;
01.07 Object 👈
- Must describe the value.
- Must be singular
//Should Do
$verifiedCustomer = $customer->verified()->first();
//Should't Do
$verified;
$data;
$resp;
$v_c;
01.08 Configs 👈
- Should use snake_case
- Must describe the value.
//Should Do
"comments_enabled"
//Should't Do
"CommentsEnabled"
"comments"
"c_enabled"
01.09 Traits 👈
- Should be an adjective.
//Should Do
"Utility"
//Shouldn't Do
"UtilityTrait"
"Utilities"
01.10 Interface 👈
- Should be an adjective or a noun.
//Should Do
"Authenticable"
//Shouldn't Do
"AuthenticationInterface"
"Authenticate"
So above, I have talked about naming convention in Laravel projects. Not only Laravel, but you guys can also use these rules with any other PHP framework.
In the next article, I will talk about another main topic of coding standards.
I hope you find my post helpful! Any feedback is greatly appreciated!
Below I mentioned my other articles. You may also read them.
Other Articles
-
MetaMask Integration With Laravel Part1 Click Here
🤩 Our Amazing Sponsors 👇View Website
View Website
View Website
-
MetaMask Integration With Laravel Part2 Click Here
Here I'm Adding a Public GitHub Repository, which will store all of my tutorials. You may clone it and see every tutorial that I will publish 🤗. GitHub Repository
Thank You Very much. --Lathindu Pramuditha-- GitHub Profile
Comments (0)