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

AndrewMast's avatar

Session based middleware not working when StartSession is enabled

My middleware code:

public function handle(Request $request, Closure $next) {
    if ($request->isMethod('get') && (!$request->hasSession() || $request->session()->pull('framed', false) == false)) {
        return response()->view('dashboard.frame');
    }

    $response = $next($request);

    if ($response instanceof RedirectResponse && $request->hasSession()) {
        $request->getSession()->flash('framed', true);
    }

    return $response;
}

This works unless I have Illuminate\Session\Middleware\StartSession in my global middleware in my Kernel. The problem is that I need StartSession for other aspects of my site to work. Does anybody know why this would be happening?

0 likes
2 replies
AndrewMast's avatar

Anybody have any ideas how I could change this to work with the StartSession middleware?

AndrewMast's avatar
AndrewMast
OP
Best Answer
Level 1

I figured it out! The problem was that I had StartSession in both $middlewareGroups['web'] and $middleware.

Please or to participate in this conversation.