Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

daddef's avatar

Laravel subdomain routing problem

Hi all, I'm trying to replicate an old site in laravel and this is my first real project with it. Everything is fine but the problem is that this site uses subdomains only to choose the database to work on. I should therefore set the routing that only works on the subdomains, only have a root for the base domain and create a function to set the database connection. I tried using the routing domain and group function but I still can't get it to work. Does anyone have any advice or a general setup of how to do it? For example, given the site "bab.test", this should refer to a view-only page, while "* .bab.test" should send to the real application with all the controllers, where * can be any word. I have already looked for solutions to this problem in other threads but I have not been able to apply the various solutions to my problem. I thank anyone who can help me :)

0 likes
4 replies
Sinnbeck's avatar

Can you show your current code that doesn't work?

daddef's avatar
Route::group(
    [
        'domain' => config('app.domain'),
        'middleware' => ['web']
    ],
    function () {
        Route::get('/', function () {
            session()->put("Mess", "Ciao");
            return view('welcome');
        })->name('start');
    }
);

Route::group(
    [
        'domain' => '{subdomain}.' . config('app.basedomain'),
        'middleware' => ['web', 'protezione'],
    ], function ($subdomain) {
    Auth::routes();
    
    Route::get('/', function () {
        session()->put("Mess", "Ciao");
        return view('welcome');
    });
    Route::get('/home', 'HomeController@index')->name('home');
    
    Route::get('/dada', function () {
        session()->put("Mess", "Ciao");
        return dd(session()->all());
        
    });  
}
);

This is the last test

1 like
daddef's avatar

Ok, I didn't know that I can have different route file. I tried to edit the RouteServiceProvider instead of the default web routing file and the result is this:

    protected function mapRootRoutes()
    {
        Route::domain(config('app.base_domain'))
            ->middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/rootweb.php'));
    }
    protected function mapWebRoutes()
    {
        Route::domain('{test}.'.config('app.base_domain'))
            ->middleware(['web', 'protezione'])
            ->namespace($this->namespace)
            ->group(base_path('routes/web.php'));
    }

Where RootWeb contains only a view and Web.php all the app route. And that is almost the full solution to the problem. The last part is to pass the wildcard to the middleware for put in the session. I tried like that

        Route::domain('{test}.'.config('app.base_domain'))
            ->middleware(['web', "protezione:{$test}"])

but give me an exception: 1

Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Undefined variable: test", 
"/home/daddef/PhpstormProjects/BabyloLaravel1/app/Providers/RouteServiceProvider.php", [])

Sorry to bother you guys but from the documentation i don't understand very well the middleware parameters. Hope you can help me to get out from this problem. Davide

1 like

Please or to participate in this conversation.