Laravel 5.2 Auth not Working

前端 未结 1 843
花落未央
花落未央 2020-11-30 11:13

As you guys know Laravel 5.2 was released a few days ago. I am trying this new version. I made a new project using the following command on CLI:

laravel new          


        
相关标签:
1条回答
  • 2020-11-30 11:58

    Laravel 5.2 introduces the middleware groups concept: you can specify that one or more middleware belongs to a group, and you can apply a middleware group to one or more routes

    By default Laravel 5.2 defines a group named web, used to group the middleware handling session and other http utilities:

    protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ],
    

    So, if you want session handling, you should use this middleware group for all the routes in which you want to use authentication:

    Route::group( [ 'middleware' => ['web'] ], function () 
    {
        //this route will use the middleware of the 'web' group, so session and auth will work here         
        Route::get('/', function () {
            dd( Auth::user() );
        });       
    });
    

    UPDATE FOR LARAVEL VERSION >= 5.2.27

    As of Laravel 5.2.27 version, all the routes defined in routes.php are using by default the web middleware group. That is achieved in app/Providers/RouteServiceProvider.php :

    protected function mapWebRoutes(Router $router)
    {
        $router->group([
            'namespace' => $this->namespace, 'middleware' => 'web'
        ], function ($router) {
            require app_path('Http/routes.php');
        });
    }
    

    So you don't need anymore to add manually the web middleware group to your routes.

    Anyhow, if you want to use the default authentication for a route, you still need bind the auth middleware to the route

    0 讨论(0)
提交回复
热议问题