Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Session Middleware broken

On my local system everything works fine, but after deploying Laravel 5.2 on our test system it looks like the session middleware is broken. Can someone help here?

Argument 1 passed to Illuminate\Session\Middleware\    
StartSession::addCookieToResponse() must be an instance of  
Symfony\Component\HttpFoundation\Response, boolean given, called in   
... /httpdocs/service/vendor/laravel/framework/src/Illuminate/Session 
/Middleware/StartSession.php on line 72 and defined

The global middlewares:

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \App\Http\Middleware\CORSMiddleware::class,
    \LucaDegasperi\OAuth2Server\Middleware\OAuthExceptionHandlerMiddleware::class
];
like image 499
DehMotth Avatar asked Mar 17 '16 12:03

DehMotth


3 Answers

I had the same problem. When investigating, I discovered that at some point in my code, I used return.

It turns out (as you can see in the end of the handle method) that after executing the handle method you should always call return $next($request);.

like image 110
Leonardo Beal Avatar answered Oct 18 '22 19:10

Leonardo Beal


I had similar problem in one of mine middleware (v5.8). 'Call to a member function SetCookie() on null', 'Add the CSRF token to the response cookies'

This was my code, working fine in 5.2, but failed in Laravel 5.8:

return view('pages.my_page')->with('data', $data);

changed to:

return response()->view('pages.my_page', ['data' => $data]);

Cheers!

like image 7
T.J. Avatar answered Oct 18 '22 17:10

T.J.


Well the addCookieToResponse method in the Illuminate\Session\Middleware\StartSession class is wanting a Response object as the first param. Make sure that you return one in all of your routes.

Here's a possible quick fix, change it to fit your case.

Before:

Route::get('hi', function() {
    return 'hi';
});

After:

Route::get('hi', function() {
    return response('hi');
});
like image 4
neochief Avatar answered Oct 18 '22 19:10

neochief