Prinsip single responsibility
Kelas dan metode seharusnya hanya memiliki satu tanggung jawab.
Contoh buruk:
|
|
Contoh terbaik:
|
|
Model tebal, controller tipis
Masukkan semua logika terkait DB ke model eloquent atau ke dalam kelas repositori jika anda menggunakan Query Builder atau kueri SQL mentah.
Contoh buruk:
|
|
Contoh terbaik:
|
|
Validasi
Pindahkan validasi dari controller ke kelas request.
Contoh buruk:
|
|
Contoh terbaik:
|
|
Business logic harus di dalam kelas services
Controller harus hanya memiliki satu tanggung jawab, jadi pindahkan business logic dari controller ke kelas service.
Contoh buruk:
|
|
Contoh terbaik:
|
|
Don’t repeat yourself (DRY)
Gunakan kembali kode ketika anda bisa. PSR membantu anda menghindari duplikasi. Juga, gunakan kembali template blade, scope eloquent, dll.
Contoh buruk:
|
|
Contoh terbaik:
|
|
Lebih memilih menggunakan Eloquent daripada menggunakan Query Builder dan query SQL mentah. Lebih memilih collections daripada array
Eloquent memungkinkan anda menulis kode yang dapat dibaca dan maintainable. Dan, Eloquent memiliki built-in tools yang bagus seperti soft deletes, events, scopes, dll.
Contoh buruk:
|
|
Contoh terbaik:
|
|
Mass assignment
Contoh buruk:
|
|
Contoh terbaik:
|
|
Jangan mengeksekusi kueri dalam template blade dan gunakan eager loading (masalah N + 1)
Contoh buruk (untuk 100 user, 101 kueri DB akan dieksekusi):
|
|
Contoh terbaik (untuk 100 user, 2 kueri DB akan dieksekusi):
|
|
Komentari kode anda, tetapi lebih baik method dan nama variabel yang deskriptif daripada komentar
Contoh buruk:
|
|
Contoh lebih baik:
|
|
Contoh terbaik:
|
|
Jangan letakkan JS dan CSS di template blade dan jangan letakkan HTML apa pun di kelas PHP
Contoh buruk:
|
|
Contoh lebih baik:
|
|
Dalam file javascript:
|
|
Cara terbaik adalah dengan menggunakan package PHP to JS
khusus untuk mentransfer data.
Gunakan file config, language, dan konstanta daripada teks dalam kode
Contoh buruk:
|
|
Contoh terbaik:
|
|
Gunakan tools standar Laravel yang diterima oleh komunitas
Selalu gunakan fungsi built-in bawaan laravel dan packages komunitas daripada menggunakan packages dan tools pihak ke-3. Developer manapun yang akan bekerja dengan aplikasi anda di masa mendatang perlu mempelajari tools baru. Dan juga, peluang untuk mendapatkan bantuan dari komunitas Laravel jauh lebih rendah saat anda menggunakan packages atau tools pihak ke-3. Jangan membuat klien Anda membayar untuk itu.
Task | Tools standar | Tools pihak ke-3 |
---|---|---|
Authorization | Policies | Entrust, Sentinel and other packages |
Compiling assets | Laravel Mix, Vite | Grunt, Gulp, 3rd party packages |
Development Environment | Laravel Sail, Homestead | Docker |
Deployment | Laravel Forge | Deployer and other solutions |
Unit testing | PHPUnit, Mockery | Phpspec, Pest |
Browser testing | Laravel Dusk | Codeception |
DB | Eloquent | SQL, Doctrine |
Templates | Blade | Twig |
Working with data | Laravel collections | Arrays |
Form validation | Request classes | 3rd party packages, validation in controller |
Authentication | Built-in | 3rd party packages, your own solution |
API authentication | Laravel Passport, Laravel Sanctum | 3rd party JWT and OAuth packages |
Creating API | Built-in | Dingo API and similar packages |
Working with DB structure | Migrations | Working with DB structure directly |
Localization | Built-in | 3rd party packages |
Realtime user interfaces | Laravel Echo, Pusher | 3rd party packages and working with WebSockets directly |
Generating testing data | Seeder classes, Model Factories, Faker | Creating testing data manually |
Task scheduling | Laravel Task Scheduler | Scripts and 3rd party packages |
DB | MySQL, PostgreSQL, SQLite, SQL Server | MongoDB |
Ikuti konvensi penamaan Laravel
Ikuti PSR standards.
Dan juga, ikuti konvensi penamaan yang diterima oleh komunitas Laravel:
What | How | Good | Bad |
---|---|---|---|
Controller | singular | ArticleController | |
Route | plural | articles/1 | |
Route name | snake_case with dot notation | users.show_active | |
Model | singular | User | |
hasOne or belongsTo relationship | singular | articleComment | |
All other relationships | plural | articleComments | |
Table | plural | article_comments | |
Pivot table | singular model names in alphabetical order | article_user | |
Table column | snake_case without model name | meta_title | |
Model property | snake_case | $model->created_at | |
Foreign key | singular model name with _id suffix | article_id | |
Primary key | - | id | |
Migration | - | 2017_01_01_000000_create_articles_table | |
Method | camelCase | getAll | |
Method in resource controller | table | store | |
Method in test class | camelCase | testGuestCannotSeeArticle | |
Variable | camelCase | $articlesWithAuthor | |
Collection | descriptive, plural | $activeUsers = User::active()->get() | |
Object | descriptive, singular | $activeUser = User::active()->first() | |
Config and language files index | snake_case | articles_enabled | |
View | kebab-case | show-filtered.blade.php | |
Config | snake_case | google_calendar.php | |
Contract (interface) | adjective or noun | AuthenticationInterface | |
Trait | adjective | Notifiable | |
Trait (PSR) | adjective | NotifiableTrait | |
Enum | singular | UserType | |
FormRequest | singular | UpdateUserRequest | |
Seeder | singular | UserSeeder |
Gunakan sintaks yang lebih pendek dan lebih mudah dibaca jika memungkinkan
Contoh buruk:
|
|
Contoh terbaik:
|
|
Contoh:
Sintaks umum | Sintaks pendek dan mudah dibaca |
---|---|
Session::get('cart') | session('cart') |
$request->session()->get('cart') | session('cart') |
Session::put('cart', $data) | session(['cart' => $data]) |
$request->input('name'), Request::get('name') | $request->name, request('name') |
return Redirect::back() | return back() |
is_null($object->relation) ? null : $object->relation->id | optional($object->relation)->id |
return view('index')->with('title', $title)->with('client', $client) | return view('index', compact('title', 'client')) |
$request->has('value') ? $request->value : 'default'; | $request->get('value', 'default') |
Carbon::now(), Carbon::today() | now(), today() |
App::make('Class') | app('Class') |
->where('column', '=', 1) | ->where('column', 1) |
->orderBy('created_at', 'desc') | ->latest() |
->orderBy('age', 'desc') | ->latest('age') |
->orderBy('created_at', 'asc') | ->oldest() |
->select('id', 'name')->get() | ->get(['id', 'name']) |
->first()->name | ->value('name') |
Gunakan IoC Container atau facades daripada kelas baru
Sintaks Kelas baru membuat penggabungan yang sempit antar kelas dan memperumit proses pengujian. Gunakan facades atau IoC container sebagai gantinya.
Contoh buruk:
|
|
Contoh terbaik:
|
|
Jangan mendapatkan data dari file .env
secara langsung
Alihkan data ke file konfigurasi sebagai gantinya dan kemudian gunakan fungsi config ()
untuk menggunakan data dalam aplikasi.
Contoh buruk:
|
|
Contoh terbaik:
|
|
Simpan tanggal dalam format standar. Gunakan accessors dan mutators untuk mengubah format tanggal
Contoh buruk:
|
|
Contoh terbaik:
|
|
Praktik bagus lainnya
Jangan pernah menaruh logika apa pun di file route
.
Minimalkan penggunaan vanilla PHP di template blade.