I know this is an old thread but maybe this may help someone in future.
The Correct way to do this is probably by utilizing the App\Providers\RouteServiceProvider. There you will find it being setup in the boot method. Here is how I utilize it by also using my config/app.php. If config is set then it uses the domain() else uses the default prefix() method.
<?php
namespace App\Providers;
...
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
...
class RouteServiceProvider extends ServiceProvider
{
...
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
if (!config('app.api'))
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
else
{
Route::domain(config('app.api'))
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
}
...
}
and here is how I do it in my config/app.php:
<?php
return [
...
/*
|--------------------------------------------------------------------------
| Application URL
|--------------------------------------------------------------------------
|
| This URL is used by the console to properly generate URLs when using
| the Artisan command line tool. You should set this to the root of
| your application so that it is used when running Artisan tasks.
|
*/
'url' => env('APP_URL', 'http://localhost'),
'api' => env('API_URL', false),
...
];