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

dharmendrarao's avatar

Session on API request

Hi All,

I am developing an ecommerce site using Angular as frontend and laravel 5 as backend. I want to use session on api request. I have changed

config/auth.php file like below


    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
],

to
   
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    'api' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
],

and in app/http/kernal.php


    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

to
   
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    'api' => [
        'throttle:60,1',
        'bindings',
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Session\Middleware\StartSession::class,
    ],
];

but when i set session on api request and get session by api request then session not set while i hit api url direclty to web browser then its working and i am getting session values.

NOTE: some data, i wanted through session and without user login

Thanks

0 likes
2 replies
bobbybouwmann's avatar

In general your API should be stateless. Why don't you just add these API routes with a session to your web group routes. This way you don't have to change anything ;) You can still prefix them with web.

2 likes

Please or to participate in this conversation.