Skip to content
Advertisement

Laravel routing: Route in domain group visible also in local environment

What I need: My app has a public domain All routes in my Admin controller should be opened only if the remote domain is domain1.com and also in local environment.

Currently: if I put the admin panel route in the group, it is not visible in local environment any more, making it difficult to develop.

// My secret domain, accessible only for admins
Route::group(['domain'=>'domain1.com'],function(){

    Route::get('admin-panel', [AppHttpControllersControlAdminController::class, 'admin_panel']);

});

// To be accessible both in domain1.com and domain2.com:
Route::get('homepage', [AppHttpControllersControlPagesController::class, 'homepage']);

Solutions

My current solution: in route file web.php I add extra line

if( App::environment() == 'local') {
    Route::get('admin-panel', [AppHttpControllersControlAdminController::class, 'admin_panel']);
}
 

but it is a crude, temporary fix.

TODO:

  • Either in route file or in the controller. A filter in a controller (for all or selected methods) would be best.
  • An if clause checking if either the environment is local or the domain is domain1.com

Thank you.

Advertisement

Answer

I think laravel not support something like that. But you can declare the routes to a function variable and then use it in each domain.

$adminRoutes = function() {
    Route::get('admin-panel', [AppHttpControllersControlAdminController::class, 'admin_panel']);
};

Route::group(array('domain' => 'domain1.com'), $adminRoutes);
Route::group(array('domain' => 'localhost'), $adminRoutes)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement