mardi 15 novembre 2016

Auth::guard('user')->user() is null in Laravel 5.3

I'm trying to pass to all templates current user object like this:

class Controller extends BaseController
{
    public function __construct()
    {
        view()->share('usr', Auth::guard('user'));
    }
}

Every controller is extended by Controller. But if i try to dump Auth::guard('user')->user() Laravel returns null, although I am logged in. Moreover when i pass this variable into template, returns current user. What I've done wrong?

my config/auth.php

'defaults' => [
    'guard' => 'user',
    'passwords' => 'users',
],
'guards' => [
        'user' => [
            'driver' => 'session',
            'provider' => 'user',
        ],
    ],
'providers' => [
        'user' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
    ],

Kernel.php

         protected $middleware = [
     \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
 ];

/**
 * The application's route middleware groups.
 *
 * @var array
 */
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,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
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,
];



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire