Back

Laravel 5.3 Route Service Provider for multiple domains


nitsuj102284 posted 6 months ago

I am using Laravel 5.3's new Route Service Provider to direct all requests from host api.example.com to to the 'routes/api.php' routes file.

protected function mapApiRoutes()
    {
        Route::group([
            'middleware' => 'api',
            'namespace' => $this->namespace,
            // 'prefix' => 'api',
            'domain' => 'api.{domain}.{tld}'
        ], function ($router) {
            require base_path('routes/api.php');
        });
    }

I also would like to use this on my test server, apitest.example.com.

'domain' => ['api.{domain}.{tld}', 'apitest.{domain}.{tld}']

Array to string conversion error.

–––––––

'domain' => 'api{test}.{domain}.{tld}'

Works only for apitest.example.com, not api.example.com.

–––––––

'domain' => 'api{test?}.{domain}.{tld}'

Does not work at all. 404.

–––––––

How can I specify multiple sub domains in this route group?

nitsuj102284 replied 6 months ago

Solved it this way:

protected function mapApiRoutes()
    {
        $callback = function ($router) {
            require base_path('routes/api.php');
        };

        //production
        Route::group([
            'middleware' => 'api',
            'namespace' => $this->namespace,
            'domain' => 'api.{domain}.{tld}'
        ], $callback);

        //test
        Route::group([
            'middleware' => 'api',
            'namespace' => $this->namespace,
            'domain' => 'apitest.{domain}.{tld}'
        ], $callback);        
    }

source: https://github.com/laravel/framework/issues/4017#issuecomment-39091856

nitsuj102284 replied 5 months ago

UPDATE. This is NOT solved. Well, not if I want to use parameters from the URI. For instance,

Route::get(test/{id}, function($id) {
    return $id     //example (where domain is api.example.local)
});

If I keep the code as is, I would have to use the route like this,

Route::get(test/{id}, function($domain, $tld, $id) {
    return $id   //999 (where id = 999)
});

Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us