Laravel global middleware can't get session

前端 未结 2 1942
梦毁少年i
梦毁少年i 2021-01-19 20:06
 protected $middleware = [
     \\App\\Http\\Middleware\\Syspoint::class,
]

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

2条回答
  •  太阳男子
    2021-01-19 20:41

    dparoli's answer correct but not exactly! Because this middleware will run every web request!

    How about running only under some route? Here is how;

    protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'sys-point' => \App\Http\Middleware\Syspoint::class,
    ];
    

    Then on your route define new middleware

    Route::group(['middleware' => ['web','sys-point'], 'namespace' => 'YourControllers'], function()
    {
    }
    

提交回复
热议问题