neoteknic
neoteknic

Reputation: 1970

Laravel Route:dispatch create new session with EncryptCookie

My code is :

…
$new_request = Request::create($page, 'GET', $request->all(), $request->cookie());
$router = Route::dispatch($new_request);
…

with middlewares

\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,

The problem : A new session is created on every request on Route::dispatch call (if I exit before, no session created, after a new session is created) : new Redis key with redis driver, or new file with file driver. But everything working correctly, it just spam new sessions keys/files instead of keeping alway the same file. If I remove the EncryptCookie, it fix the problem, only one file is used, no more file after each request...

How to fix that with EncryptCookies enabled ?

Upvotes: 0

Views: 474

Answers (1)

neoteknic
neoteknic

Reputation: 1970

Problem fixed, the problem is the source code have :

\App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class,

and the request have these middleware too. Using encrypt/startsession two times seem to do that.

The simple solution is to remove these middleware before the source code with Route:dispatch, but leave it in the Request.

Upvotes: 0

Related Questions