Laravel Coding Standard

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Laravel Coding Standard

Table of Contents
1. Follow Laravel naming conventions
2. Use shorter and more readable syntax where possible
3. Single responsibility principle
4. Fat models, skinny controllers
5. validation
6. Business logic should be in service class (Interface and Repository if third party service include so type in
services folder inside make for particular service like : email service,payment service etc)
7. Don't repeat yourself (DRY)
8. Prefer to use Eloquent over using Query Builder and raw SQL queries. Prefer collections over arrays
(Request->all())
9. Mass assignment
10. Do not execute queries in Blade templates and use eager loading (N + 1 problem)
11. Do not put JS and CSS in Blade templates and do not put any HTML in PHP classes
12. Use config and language files, constants instead of text in the code
13. Use standard Laravel tools accepted by community
14. Use IoC container or facades instead of new Class
15. Do not get data from the ".env" file directly
16. Store dates in the standard format. Use accessors and mutators to modify date format
17. Other good practices
1. Follow Laravel Naming Conventions
So, follow naming conventions accepted by Laravel community:

What How Good Bad


Controller Singular ArticalController ArticalsController
route Plural articles/1 article/1
model Singular Users users
hasOne or belongsTo Singular articleComment articleComments,
relationship article_comment
All other Plural articleComments articleComment,
relationships article_comments
Foreign key singular model name article_id ArticleId, id_article,
with _id suffix articles_id
migration - 2017_01_01_000000 2017_01_01_000000
_create_articles_tabl _articles
e
Method camel case getAll get_all
Method in test class camel case testGuestCannotSee test_guest_cannot_s
Article ee_article
What How Good Bad
Collection descriptive, plural $activeUsers = $active, $data
User::active()->get()
objects descriptive, singular $activeUser = $users, $obj
User::active()->first()
views kebab case show- showFiltered.blade.p
filtered.blade.php hp,
show_filtered.blade.
php
configuration kebab case google-calendar.php googleCalendar.php,
google_calendar.php
Contract (interface) adjective or noun Authenticatable AuthenticationInterf
ace, IAuthentication
trait adjectives Notifiable NotificationTrait
2. Use shorter and more readable syntax where possible
3. Single responsibility principle
A class and a method should have only one responsibility.
Bad:
public function getFullNameAttribute()
{
if (auth()->user() && auth()->user()->hasRole('client') && auth()->user()->isVerified())
{
return 'Mr. ' . $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name;
} else {
return $this->first_name[0] . '. ' . $this->last_name;
}
}
Good:
public function getFullNameAttribute()
{
return $this->isVerifiedClient() ? $this->getFullNameLong() : $this->getFullNameShort();
}
public function isVerifiedClient()
{
return auth()->user() && auth()->user()->hasRole('client') && auth()->user()-
>isVerified();
}
public function getFullNameLong()
{
return 'Mr. ' . $this->first_name . ' ' . $this->middle_name . ' ' . $this-
>last_name;
}
public function getFullNameShort()
{
return $this->first_name[0] . '. ' . $this->last_name;
}
4. Fat models, skinny controllers
Put all DB related logic into Eloquent models or into Repository classes if you're using Query
Builder or raw SQL queries.
Bad:

public function index()


{
$clients = Client::verified() ->with(['orders' => function ($q) {
$q->where('created_at', '>', Carbon::today()->subWeek());
}]) ->get();
return view('index', ['clients' => $clients]);
}

Good:
public function index()
{
return view('index', ['clients' => $this->client->getWithNewOrders()]);
}
class Client extends Model
{
public function getWithNewOrders()
{
return $this->verified() ->with(['orders' => function ($q) {
$q->where('created_at', '>', Carbon::today()->subWeek());
}]) ->get();
}
}

5. validation
Note: Move validation from controllers to Request classes.
Bad:
public function store(Request $request)
{
$request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
'publish_at' => 'nullable|date',
]);
}
Good:

public function store(PostRequest $request)


{
....
}

class PostRequest extends Request


{
public function rules()
{
return [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
'publish_at' => 'nullable|date',
];
}
}
7. Don't repeat yourself (DRY)
Reuse code when you can. SRP is helping you to avoid duplication. So, reuse Blade
templates, use Eloquent scopes etc.
Bad:
public function getActive()
{
return $this->where('verified', 1)->whereNotNull('deleted_at')->get();
}
public function getArticles()
{
return $this->whereHas('user', function ($q) {
$q->where('verified', 1)->whereNotNull('deleted_at');
})->get();
}

Good:
public function scopeActive($q)
{
return $q->where('verified', 1)->whereNotNull('deleted_at');
}
public function getActive()
{
return $this->active()->get();
}
public function getArticles()
{
return $this->whereHas('user', function ($q)
{
$q->active();
})->get();
}
9. Mass assignment
Bad:
$article = new Article;
$article->title = $request->title;
$article->content = $request->content;
$article->verified = $request->verified;
// Add category to article
$article->category_id = $category->id;
$article->save();

Good:
$category->article()->create($request->all());

10. Do not execute queries in Blade templates and use eager loading
(N + 1 problem)
Bad (for 100 users, 101 DB queries will be executed):
@foreach (User::all() as $user)

@endforeach
Good (for 100 users, 2 DB queries will be executed):

$users = User::with('profile')->get();

@foreach ($users as $user)

@endforeach
11. Do not put JS and CSS in Blade templates and do not put any HTML
in PHP classes
Bad:
let article = ``;

Better:
<input id="article" type="hidden" value="">
Or
<button class="js-fav-article" data-article=""><button>

In a Javascript file:

let article = $('#article').val();


12. Use config and language files, constants instead of text in the code
Bad:
public function isNormal()
{
return $article->type === 'normal';
}
return back()->with('message', 'Your article has been added!');

Good:
public function isNormal()
{
return $article->type === Article::TYPE_NORMAL;
}
return back()->with('message', __('app.article_added'));

13. Do not get data from the ".env" file directly


Pass the data to config files instead and then use the config() helper function to use the
data in an application.
Bad:
$apiKey = env('API_KEY');

Good:
// config/api.php

'key' => env('API_KEY'),

// Use the data

$apiKey = config('api.key');
14. Store dates in the standard format. Use accessors and mutators
to modify date format

Good:
// Model
protected $dates = ['ordered_at', 'created_at', 'updated_at'];

public function getSomeDateAttribute($date)


{
return $date->format('m-d');
}

15. Other good practices


Note : Never put any logic in routes files.
Minimize usage of vanilla PHP in Blade templates.
Important: 1) We have only used interfaces and repository for divide and concern
part on coding.
2) Were we used helper if make small function like date format change and time
or language change so used not used inside helper query.
3) Service folder mostly used to implement third part service when we used in our
project
https://accesto.com/blog/solid-php-solid-principles-in-php/

Thank You

You might also like