Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel global middleware can't get session

 protected $middleware = [
     \App\Http\Middleware\Syspoint::class,
]

use Session;
class Syspoint
{
    echo \Session::get('syspoint');
}

I have a middleware required run everytime when page request, the middleware contain session.

I place inside of protected $middleware, but global middleware not able to get session.

like image 314
Benjamin W Avatar asked Mar 18 '17 17:03

Benjamin W


1 Answers

You are calling Session but it is not already started.

If you need Session inside your middleware you have to put it in the property protected $middlewareGroups under the key web and after the call to StartSession, i.e.:

 protected $middlewareGroups
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \App\Http\Middleware\Syspoint::class,
like image 51
dparoli Avatar answered Nov 09 '22 22:11

dparoli