G
G
Gramcoder2020-02-09 19:41:15
Laravel
Gramcoder, 2020-02-09 19:41:15

Why can't I set a new value in the session in the middleware?

Why doesn't the middleware set a new value in the session? The middleware itself is in the global middleware group:

protected $middleware = [
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\MyMiddleware::class,
    ];

The intermediary:
// \App\Http\Middleware\MyMiddleware.php
    public function handle($request, Closure $next)
    {
        session()->put('last_request_time', time());

        return $next($request);
    }

It doesn't work, and I don't understand why. Of course, if the intermediary is placed in the "web" group, then everything works successfully, but if the request is made to an indefinite page (not found), then this intermediary will no longer work, and the value in the session will not change.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2020-02-09
@JhaoDa

Do you see in the list of global middleware the middleware that starts the session and that is in the group web? That's what I don't see. Is the hint enough?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question