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

rst630's avatar

Why api requests applying web middleware?

I know that I need send Accept: application/json for laravel treat this request as api call

But on top level domains it's working w/o this header on all requests.

On subdomains it's treat this request as web request.

Few examples:

RouteServiceProvider.php:

    protected function mapApiRoutes()
    {
	// api on top level domain
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));

	// api on subdomain
        Route::prefix('api/partner')
            ->middleware('auth:partner-api-jwt')->withoutMiddleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/partner_api.php'));
    }

api.php // top level

Route::group(['middleware' => 'auth:admin-api-jwt'], function() {
    Route::post('/admin/auth', [\App\Http\Controllers\AdminController::class, 'getAuth']);
});

^^^^ this route will work even if I don't set Accept header

partner_api.php:

Route::domain(config('domains.partner'))->group(function() {
    Route::middleware('auth:partner-api-jwt')->group(function() {
        Route::apiResource('stocks', StockController::class)
            ->parameters(['stocks' => 'partner_stock'])
            ->except('show');
    });
});

^^^^ this route w/o Accept header will work only for GET request. When trying to POST/PUT/DELETE - it's enabling web middleware and redirect as unauthentificated. It even ignoring withoutMiddleware('web') - does't matter where to place it in partner_api.php or in RouteServiceProvider.php.

So I think this is a BUG! or not well documented, because domain/subdomain has different behavior in this case. Even GET vs POST has different behavior.

How I can totally disable web middleware for api routes?

Maybe laravel care only about default api.php that's why it working fine?

0 likes
0 replies

Please or to participate in this conversation.