\Auth::user() 在 5.3.6 中为空?

\Auth::user() is null in 5.3.6?

这大约是 Laravel 5.3.6

我可以登录成功,登录后可以查看Auth User。我可以显示 Auth::guard() 具有当前用户对象的确切位置。以下是详情。

  1. Go to Login Controller
  2. Go to AuthenticatesUsers Trait
  3. Go to sendLoginResponse method. User reaches here successfully because user is authenticated successfully.

在这里我可以检查 $this->guard()->user() 是否有当前用户值。但是当控制到达角色控制器时....我试图像这样访问它 dd(Auth::guard()); 并且值为空。我还在 Role Controller 中添加了下面的参考。

use Illuminate\Support\Facades\Auth;

下面是我的 Role Controller 路由。

Route::group(['middleware' => ['auth']], function () {
    Route::get('/Roles',              
        array (
            'uses'  => 'Website\Role\RoleController@index',      
            'as'    => 'Roles'
        )
    );
});

你在Laravel 5.3.6遇到过这种问题吗?

\Auth::guard() 的输出如下。

SessionGuard {#162 ▼
  #name: "web"
  #lastAttempted: null
  #viaRemember: false
  #session: Store {#165 ▶}
  #cookie: CookieJar {#167 ▶}
  #request: Request {#40 ▶}
  #events: Dispatcher {#5 ▶}
  #loggedOut: false
  #tokenRetrievalAttempted: false
  #user: null
  #provider: EloquentUserProvider {#158 ▶}
}

Kernel.php 文件如下所示

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

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',
    ],
];

我改成了下面这样

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \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,
];

protected $middlewareGroups = [
    'web' => [
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

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