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

tylerssn's avatar

Routing by Domain via Domains in Database

I need to route domains dynamically by domains pulled from my database. My current setup looks like this:

// domain handler
Route::domain('{userDomain}', function(){
    Route::get('{fullPath}', function($domain, $fullPath){
           // If the domain in the database handle it with the $fullPath
    })
        ->where('fullPath', '.*');
});

// Typical app routes
Route::get('/', ...)
Route::get('/home', ...)

The problem that I am encountering is that my "typical" app routes and "domain-handled" routes conflict with one another. For example, the domain-handled routes may have a path of / which is also used by my homepage making handled-domain.com/ conflict with myapp.com/.

I thought I had this fixed by creating a middleware and adding this to my RouteServiceProvider::map():


    /**
     * Define the routes for the application.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function map(Router $router)
    {
	if(!$this->isMainAppDomain()) {
            $this->mapVanityRoutes($router);
            return;
        }

        $this->mapWebRoutes($router);

        $this->mapApiRoutes($router);

        //
    }

The problem with this is that it is incompatible with tests. It seems the app is initiated with my APP_URL (main app URL) and in tests $this->refreshApplication() does not help.

In short, how do I route by domains in my database and prevent and prevent my main app routes from conflicting with these requests?

P.S. I tried Route::domain on my main app URLs, but this doesn't impact routes generated for the web middleware by vendors.

0 likes
2 replies
tylerssn's avatar

Perhaps this could be resolved by mapping APP_URL to my web middleware? Is this possible

tylerssn's avatar

I think this can be resolved if I can determine why, in my tests, $this->get(url) behaves as if all requests are to APP_URL as far as the service provider is concerned.

Otherwise it seems to indicate that the service provider is not a good place for this logic to live.

Please or to participate in this conversation.