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

movepixels's avatar

Laravel API Routes

I am trying to make the laravel api routes go to a subdomain but cant seem to find anything that solves what I am trying to do.

My front end needs to point to api.site.com/v1

My routes api.php file wraps everything like so:

Route::group(['domain' => 'api', 'prefix' => 'v1'], function () {  
...all the site routes / endpoints 

But I can not seem to get that going.

Any help / insight would be greatly appreciated.

Thanks,

Dave

0 likes
3 replies
movepixels's avatar

I ended up going an alternate way. In RouteServiceProvider.php :

protected function mapApiRoutes()
    {
        Route::domain(env('API_ENDPOINT'))
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }

But it accomplishes the same thing. Your way is correct and its also did work!

Thanks for your time.

Dave

usman's avatar

+1 @adrian.nuernberger, also if you are using a web server you will need to configure the virtual hosts accordingly pointing to the same laravel installation. However when developing using artisan serve, you can test it like:

Route::domain('api.localhost')->group(function() {
    Route::get('', function() {
        return 'hello api';
    });
});

Route::get('', function() {
    return 'hello web';
});

Now pointing browser to api.localhost:8000 and localhost:8000 should return the hello api and hello web response respectively.

Please or to participate in this conversation.