Memisahkan Routing agar lebih mudah untuk di-maintain di Laravel (Separate Routing in Laravel)

Laravel is a web application framework with expressive, elegant syntax. A web framework provides a structure and starting point for creating your application, allowing you to focus on creating something amazing while we sweat the details.

Posted on 01/01/2022

author
Wahidin Aji

Singkat saja, sederhananya didalam routing laravel biasanya kita menempatkannya pada 1 file web.php. secara harfiah memang seperti itu, akan tetapi ketika project sudah besar dan ada berbagai macam routing, terkadang itu akan membuat kita merasa kesulitan untuk menemukan routing mana yang akan kita edit. Contoh,

Route::prefix('front')->name('front.')->middleware('auth','user')->group(function (){
  /**
   * your routing for public page here.
  */
});
Route::prefix('backoffice')->name('backoffice.')->group(function () {
    Route::get('login', [BackofficeLoginController::class, 'index'])->name('login');
    Route::post('authenticate', [BackofficeLoginController::class, 'authenticate'])->name('authenticate');
});
 
Route::prefix('seller')->name('seller.')->group(function () {
    Route::get('login', [SellerLoginController::class, 'index'])->name('login');
    Route::post('authenticate', [SellerLoginController::class, 'authenticate'])->name('authenticate');
    Route::get('register', [SellerRegisterController::class, 'index'])->name('register');
    Route::post('register', [SellerRegisterController::class, 'store'])->name('register.store');
});
Route::group(['middleware' => ['auth']], function () {
    Route::prefix('account')->name('account.')->group(function () {
        Route::get('faqs', [FaqController::class, 'index'])->name('faqs');
        Route::get('profile', [ProfileController::class, 'index'])->name('profile');
        Route::get('edit-profile', [ProfileController::class, 'edit'])->name('profile.edit');
        Route::post('profile', [ProfileController::class, 'update'])->name('profile.update');
        Route::post('update-avatar', [ProfileController::class, 'avatarUpdate'])->name('avatar.update');
        Route::get('change-password', [ChangePasswordController::class, 'index'])->name('change-password');
        Route::post('change-password', [ChangePasswordController::class, 'update'])->name('change-password.update');
        Route::get('user-manuals', [UserManualController::class, 'index'])->name('user-manuals');
        Route::get('user-manuals/{id}/download', [UserManualController::class, 'download'])->name('user-manuals.download');
        Route::get('notifications', [NotificationController::class, 'index'])->name('notifications');
        Route::get('notifications/{id}', [NotificationController::class, 'show'])->name('notifications.show');
    });
    Route::namespace('Systems')->prefix('systems')->name('systems.')->group(function () {
      /**
       * your routing for systems panel page here.
      */
    });
    Route::namespace('Backoffice')->name('backoffice.')->prefix('backoffice')->group(function () {
      /**
       * your routing for backoffice panel page here.
      */
    });
});

mungkin contohnya terlihat sedikit complex. sebentar, saya buatkan contoh yang lebih sederhana.

Gambar 1. Contoh routes sebelum kita mapping
Gambar 1. Contoh routes sebelum kita mapping
pada gambar diatas ini 👆, terlihat ada 2 routing di dalam 1 file web.php. nah, biasanya agar lebih mudah di mapping ketika terjadi perubahan pada routing tertentu, saya biasanya memisahkannya kembali menjadi 1 file berbeda. dari yang sebelumnya struktur file-folder nya seperti ini :

.
 ├── routes
 |   ├── api.php
 |   ├── channels.php
 |   ├── console.php
 |   ├── web.php
 |

menjadi

.
 ├── routes
 |   ├── admin  <-- untuk ini, saya lebih suka pakai nama web karena didalamnya ada beberpa file routing. untuk kali ini saya spesifikkan saja biar jelas
 |   |   ├── admin.php
 |   ├── api.php
 |   ├── channels.php
 |   ├── console.php
 |   ├── web.php
 |

setelah itu, pada gambar 1 line 6-8 kita pindahkan ke admin.php.

Gambar 2. Routes untuk halaman admin
Gambar 2. Routes untuk halaman admin

lah kok cuma line 6-8? ya sabarr....

nahh, selanjutnya ini untuk naming dan prefixing kita atur di pengaturan routing. (pengaturan) 😁 nya ada di folder app/Providers/RouteServiceProvider.php, lihat code pada line 50-55

Gambar 3. RouteServiceProvider pada Laravel
Gambar 3. RouteServiceProvider pada Laravel
di sanalah kita meletakkan name x prefixnya.

untuk uji coba silahkan lihat pada beberapa gambar ini,

Sebelum kita pisah

  • adminpage

    Gambar 4. Routing untuk halaman admin sebelum kita pisahkan
    Gambar 4. Routing untuk halaman admin sebelum kita pisahkan

  • homepage

    Gambar 5. Routing untuk halaman beranda sebelum kita pisahkan
    Gambar 5. Routing untuk halaman beranda sebelum kita pisahkan

Setelah kita pisah

  • adminpage

    Gambar 6. Routing untuk halaman admin setelah kita pisahkan
    Gambar 6. Routing untuk halaman admin setelah kita pisahkan

  • homepage

    Gambar 7. Routing untuk halaman admin setelah kita pisahkan
    Gambar 7. Routing untuk halaman admin setelah kita pisahkan

Repositori project

what's next? if you have some req tips. reach me on twitter @a17wahidin

PHP IS STILL ALIVE 😆.