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

MirasMustimuly's avatar

Should images be served via api or web routes?

Hello!

I have a route that serves images like this


return response()
            ->stream(function () use ($final_image) {
                echo $final_image;
            }, 200, [
                'Content-Type' => 'image/jpeg',
                'Cache-Control' => 'max-age=259200,immutable'
            ]);

When i make a request to get this image i see some headers that I dont think i need when serving images. For example set cookie with xsrf token and another set cookie with session and also i have Vary: X-Inertia header.

I guess it is because my route is in web.php should i move that route to api.php?

Are file serving routes supposed to be served via api routes?

0 likes
2 replies
martinbean's avatar
Level 80

@mirasmustimuly I usually put routes for things like generating thumbnails in a “stateless” web route group:

$this->routes(function () {
    Route::middleware('api')
        ->prefix('api')
        ->group(base_path('routes/api.php'));

    Route::middleware('web')
        ->group(base_path('routes/web.php'));

    // Add this group...
    Route::middleware([])
        ->group(base_path('routes/web.stateless.php'));
});

That way no session is booted, no cookies are set/read, etc as something like serving a thumbnail image doesn’t require state.

1 like

Please or to participate in this conversation.