Hi guys, From this article I'm starting to describe you to about Laravel [PHP] coding Standards with PSR .
To make this tutorials much easier, I will divide full tutorial as number or articles. So this is first articles of them.
Article 1 : Naming Conventions. โ
Here we will talk about naming conventions about PHP. Following conventions have accepted by Laravel community.
01.01 Controller ๐
Name should be in singular form.
Should use PascalCase .
Should Do
Should't Do
CustomerController.php
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 single part For best Practice.
Should Do
Should't Do
/customers/25
customer/25
/customers/password-reset
/customers/password_reset
"
/customers/passwordReset
01.02.02 Route Name ๐
Should use snake_case with dot notation.
Better to use same name like in URL.
Should Do
Should't Do
->('customers.view')
->('customers-view')
"
->('customers_view')
->('customers.password_reset')
->('customers.password.reset')
"
->('customers.password-reset')
"
->('customer-password-reset')
01.03 DataBase Related ๐
01.03.01 Migration ๐
Should use name as what you want to do with snake_case .
Should Do
Should't Do
2021_03_19_033513_create_customers_table.php
2021_03_19_033513_customers.php
2021_03_19_033513_add_image_id_to_customers_table.php
2021_03_19_033513_add_image_id_customers.php
2021_03_19_033513_drop_image_id_from_customers_table.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
Should't Do
customers
customer
cart_items
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
Should't Do
course_student
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 use for better practice.
Should Do
Should't Do
first_name
user_first_name
, FirstName
01.03.05 Foreign key ๐
Should use snake_case .
Should use singular table name with id prefix.
Should Do
Should't Do
course_id
courseId
, id
,courses_id
,id_course
01.03.06 Primary key ๐
Should Do
Should't Do
id
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
Should't Do
Customer
Customers
,customer
01.03.08 Model Single relations [Has One, Belongs To] ๐
Method name must be in singular form.
Should Use camalCase
Should Do
Should't Do
studentCourse
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
Should't Do
cartItems
CartItem
,cart_item
,cartItem
01.04 Functions ๐
Should Do
Should't Do
show_route
showRoute
,ShowRoute
01.05 Methods in resources controller ๐
Should use camelCase
Must use singles words related to action
Should Do
Should't Do
store
saveCustomer
show
viewCustomer
destroy
deleteCustomer
index
allCustomersPage
01.06 Variables ๐
Should use camelCase
Must use readable words which are describe about value.
Should Do
Should't Do
$customerMessages
$CustomerMessages
,$customer_messages
, $c_messages
, $c_m
01.07 Collection ๐
Must described about the value.
Must be plural
Should Do
Should't Do
$verifiedCustomers = $customer->verified()->get()
$verified
,$data
, $resp
, $v_c
01.07 Object ๐
Must described about the value.
Must be singular
Should Do
Should't Do
$verifiedCustomer = $customer->verified()->first()
$verified
,$data
, $resp
, $v_c
01.08 Configs ๐
Should use snake_case
Must described about the value.
Should Do
Should't Do
comments_enabled
CommentsEnabled
,comments
, c_enabled
, $ce
01.09 Traits ๐
Should Do
Should't Do
Utility
UtilityTrait
,Utilities
01.10 Interface ๐
Should be adjective or a noun.
Should Do
Should't Do
Authenticable
AuthenticationInterface
,Authenticate
So above I have talked about naming convetion in Laravel projects. not only Laravel you guys can use this rules with any other PHP framework.
With next article I will talk about another main topic of coding standards.
I hope you find my post useful! Any feedback is greatly appreciated!
below I mentioned my other articles. you may also read them.
You may Find my Fiver Gig Here.
https://www.fiverr.com/s2/0c68721323
Other Articles
Here I'm Adding Public GitHub Repository which will store all of my tutorials. you may clone it and see every tutorials what I will publish ๐ค.
GitHub Repository
Tutorials
Here I will show all the code blocks of my tutorials. you can copy anything or learn anything.
Articles
Thank You Very much .
--Lathindu Pramuditha--
GitHub Profile
It's About Lathindu Pramuditha's Account
เถเถบเทเถถเทเทเถฑเท (Welcome)๐๐ป, I'm Lathindu Pramuditha Amarasekara!
Founder, CEO AT Axcertro
A little more about me...
namespace App \Models
use Illuminate \Database \Eloquent \Factories \HasFactory ;
use Illuminate \Database \Eloquent \Life ;
class ProfileOfLathindu extends Life
{
use HasFactory ;
const LANGUAGES = [
'JAVASCRIPT ' => 1 ,
'PHP ' => 2 ,
'PYTHON ' => 3 ,
'SOLIDITY ' => 4 ,
'DART ' => 5
];
const FRAMEWORKS = [
'NextJs ' => 1 ,
'LARAVEL ' => 2 ,
'FLUTTER ' => 3 ,
'DJANGO ' => 4 ,
'ANGULAR ' => 5 ,
'IONIC ' => 6
];
const EXPERIENCE = 'xxxxxxxxxx of hours from 2017 ' ;
const MORE_EXPERIENCE = [
'PAYPAL_API ' => 1 ,
'STRIPE_API ' => 2 ,
' โฆ
Enter fullscreen mode
Exit fullscreen mode
Top comments (3)
Great post! You should repost it on the DevDojo site and you will have the chance to win a weekly prize!
Thank you very much Bobby lliev
I love the insight. Thank you.