9

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
];
DehMotth
  • 679
  • 3
  • 12
  • 21

6 Answers6

10

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);.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Leonardo Beal
  • 724
  • 11
  • 24
  • Had the same problem by mistakenly commenting out ```return $next($request);``` at the end of the function – Ulterior Jan 29 '18 at 08:38
  • Taylor Towell says it is fixed since 5.1.1 https://github.com/laravel/lumen-framework/issues/117 but I have this error (I return error) in 5.3. Thanks for the workaround – shukshin.ivan Jun 28 '18 at 14:32
7

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!

T.J.
  • 71
  • 1
  • 2
4

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');
});
neochief
  • 569
  • 4
  • 6
0

In my case it was just cache. try running

php artisan config:cache
Matteus Barbosa
  • 2,409
  • 20
  • 21
0

To all the people coming for this error, it's the cookie that fails.

  1. So the fastest fix is to use another browser.

  2. Go to settings in your browser and find the cookie and delete it.

George_kane
  • 305
  • 3
  • 4
  • 17
0

I had the same issue, turned out the issue for me was I changed the domain in my config/session.php in my production server


    'domain' => env('SESSION_DOMAIN', 'mydomain.com'),

on my development server which use 127.0.0.1 can't access my domain

change it to


    'domain' => env('SESSION_DOMAIN', null),

and now working fine

PHANTOM-X
  • 502
  • 6
  • 16