Dasar-Dasar Routing
Apa itu Routing?
Routing adalah mekanisme untuk menentukan bagaimana aplikasi merespons permintaan klien ke
endpoint tertentu.
Dalam Laravel 12, routing didefinisikan dalam file routes/web.php atau routes/api.php.
Route Modern Laravel 12
Urutan Route & Penulisan yang Benar
🎯 Prinsip Penting: Route dibaca dari ATAS ke BAWAH
Laravel akan mencocokkan route pertama yang sesuai dengan URL. Jika route sudah cocok, route di bawahnya tidak akan diperiksa lagi.
❌ SALAH - Route Bentrok
URL /user/profile akan ditangkap oleh
route pertama dengan id = "profile"
✅ BENAR - Urutan yang Tepat
Sekarang /user/profile akan ditangani
dengan benar
📋 Tips Menghindari Bentrok Route:
- 1. Route Spesifik di Atas: Letakkan route dengan path tetap (seperti
/user/profile) sebelum route dengan parameter (seperti/user/{id}) - 2. Gunakan Constraints: Batasi parameter dengan regex untuk menghindari konflik
- 3. Grouping yang Logis: Kelompokkan route berdasarkan fungsi dan tingkat spesifikasi
- 4. Test Urutan: Selalu test route dengan
php artisan route:list - 5. Dokumentasi: Beri komentar pada route yang berpotensi konflik
Route Resource - Route Gabungan Laravel
Route Resource adalah fitur Laravel yang memungkinkan kita membuat semua route CRUD (Create, Read, Update, Delete) dengan satu baris kode saja. Ini sangat menghemat waktu dan mengikuti konvensi RESTful secara otomatis.
Mengapa Menggunakan Resource Routes?
- • Membuat 7 route sekaligus untuk operasi CRUD
- • Mengikuti konvensi RESTful secara otomatis
- • Menghemat waktu dan mengurangi kode berulang
- • Konsisten dengan standar Laravel
- • Mudah di-maintain dan dipahami developer lain
Jenis Resource Routes:
- •
Route::resource()- Web routes dengan form - •
Route::apiResource()- API routes tanpa form - • Nested resources untuk relasi
- • Partial resources (only/except)
Web Resource Routes (7 routes):
- •
GET /users→ index (daftar semua) - •
GET /users/create→ create (form tambah) - •
POST /users→ store (simpan data) - •
GET /users/{user}→ show (detail) - •
GET /users/{user}/edit→ edit (form edit) - •
PUT/PATCH /users/{user}→ update (update data) - •
DELETE /users/{user}→ destroy (hapus)
API Resource Routes (5 routes):
- •
GET /posts→ index - •
POST /posts→ store - •
GET /posts/{post}→ show - •
PUT/PATCH /posts/{post}→ update - •
DELETE /posts/{post}→ destroy - • Tidak ada create & edit forms
Debugging & Monitoring Route:
Command untuk Cek Route:
Route Cache (Production):
Best Practice File Organization:
- •
routes/web.php- Route untuk halaman web (middleware: web) - •
routes/api.php- Route untuk API (middleware: api, prefix: /api) - •
routes/admin.php- Route khusus admin (custom file) - •
routes/auth.php- Route autentikasi (Laravel UI/Breeze)
Nested Resources:
Contoh: /users/1/posts (index),
/posts/5 (show dengan shallow)
Named Routes - Penamaan Route
Named Routes adalah fitur Laravel yang memungkinkan kita memberikan nama pada route sehingga mudah direferensikan dalam aplikasi. Ini sangat berguna untuk membuat URL yang dapat berubah tanpa harus mengubah semua kode yang mereferensikannya.
✅ Keuntungan Named Routes:
- • URL bisa berubah tanpa error
- • Code lebih mudah dibaca
- • Mudah maintenance dan refactoring
- • Auto-complete di IDE
- • Type safety di Laravel 12
🎯 Kapan Menggunakan:
- • Link navigation menu
- • Form action URLs
- • Redirect dalam controller
- • Email templates
- • API documentation