\ Auth :: user()在5.3.6中是否为null? [英] \Auth::user() is null in 5.3.6?

查看:140
本文介绍了\ Auth :: user()在5.3.6中是否为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够成功登录,并且可以在登录后检查Auth User.我可以显示Auth::guard()具有当前用户对象的确切位置.下面是详细信息.

I am able to login successfully and I can check Auth User after login. I can show the exact location where Auth::guard() has current user object. Below are the details.

  1. 转到登录控制器
  2. 转到AuthenticatesUsers特质
  3. 转到sendLoginResponse方法.用户已成功通过身份验证,因此已成功到达此处.
  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());来访问它,并且值是null.我还在下面的角色控制器"中添加了参考.

here I can check $this->guard()->user() has current user value. But when control reaches to Role controller....I tried to access it like this dd(Auth::guard()); and value was null. I also added reference below in Role Controller.

use Illuminate\Support\Facades\Auth;

下面是我去角色控制器的路线.

Below is my route for Role Controller.

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

您在Laravel 5.3.6中是否遇到过此类问题?

Did you face this kind of issue in 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',
    ],
];

这篇关于\ Auth :: user()在5.3.6中是否为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆