Laravel 8 Routing Subdomain

Viewed 6617

I'm new to using Laravel 8. In previous Laravel version 7, we can pass subdomain name with this way

    Route::group( [ 'domain' => '{admin}.example.com' ], function () {
        Route::get('/index', 'HomeController@index($account)' );
    }

But, in Laravel 8 structure code to call Controller was changed like this.

    Route::domain('{admin}.example.com')->group(function () {
        Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
    });

I've been looking for the docs but have not found them. Can you show me docs or helping me to send / passing subdomain from web.php to Controller

1 Answers

You can simply add a parameter to your controller method with the same name as the route parameter. Laravel takes care of binding the variable behind the scenes.

It's not explicitly shown in the docs with reference to controllers, but there is a basic example. https://laravel.com/docs/8.x/routing#route-group-subdomain-routing

Below is an example with a controller.

Route::domain('{subdomain}.example.com')->group(function () {
    Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
});
class HomeController 
{
    public function index($subdomain)
    {
        dd($subdomain);
    }
}

://admin.example.com/home

"admin"
Related