Auth :: user()在新路由中为null

梅迪·甘卡

我正在使用laravel 6并在我的应用中有2条路线; 索引和仪表板。我的routes/web是:

Auth::routes();
Route::middleware(['auth'])->group(function () {
    Route::get('/index', 'todoApp\TodoController@index')->name('index');
    Route::get('/dashboard', 'todoApp\Dashboard@dashboard')->name('dashboard');
});

我最近添加了仪表板路线。Auth::user()当我将其转储到仪表板路由中但不包含在索引中时为null。那是什么

滞后箱

在中间件堆栈运行之前,实例化了您的控制器;这就是Laravel如何知道您通过构造函数设置了什么中间件的方法。因此,您此时将无法访问已验证的用户或会话。例如:

public function __construct()
{
    $this->user = Auth::user(); // will always be null
}

如果您需要分配此类变量或访问此类信息,则需要使用控制器中间件,该中间件将在StartSession中间件之后的堆栈中运行

public function __construct()
{
    $this->middleware(function ($request, $next) {
        // this is getting executed later after the other middleware has ran
        $this->user = Auth::user();

        return $next($request);
    });
}

dashboard调用方法时,中间件堆栈已将请求一直传递到堆栈的末尾,因此Auth在该位置运行并可用的所有中间件都已经在运行,这就是为什么您可以访问该中间件Auth::user()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Laravel 5.2中auth()-> user()为null

来自分类Dev

Auth :: user()返回null

来自分类Dev

$ auth为空的问题

来自分类Dev

Laravel Auth :: user()关系

来自分类Dev

Auth :: user()在Laravel 5.2中返回null

来自分类Dev

NOT NULL约束失败:auth_user.password

来自分类Dev

Ember ember-simple-auth覆盖routeIfAlreadyAuthenticated在应用程序路由中

来自分类Dev

Ember CLI-在路由中注入服务并使用ember-simple-auth获取当前用户

来自分类Dev

SQLDecodeError位于/ admin / auth / user /

来自分类Dev

Django的auth_user_model

来自分类Dev

Laravel 5.3获取尝试为true,但是Auth :: User返回null吗?

来自分类Dev

django_auth_ldap custom AUTH_USER_MODEL IntegrityError

来自分类Dev

使用哪个Auth :: check()或Auth :: user()-Laravel 5.1?

来自分类Dev

找不到类'Illuminate\Foundation\Auth\User' JWT Auth Laravel

来自分类Dev

Laravel $auth 调用函数为 $auth->functionname 而不是 $auth->function()

来自分类Dev

如何在主路由中加载新路由

来自分类Dev

一旦在 HAPI 框架的后续路由中完成身份验证,如何填充 request.auth.isAuthenticated?

来自分类Dev

我在Laravel中获得Auth :: user为空

来自分类Dev

无法设置设置。在Django中为AUTH_USER_MODEL

来自分类Dev

当auth是父代时,Yii user-> checkAccess为false

来自分类Dev

我在Laravel中获得Auth :: user为空

来自分类Dev

Web和Api路由的Laravel Auth问题

来自分类Dev

登录自定义路由被Auth拒绝

来自分类Dev

如何创建类似于 Auth::routes() 的路由

来自分类Dev

Angular Auth Guard 安全路由

来自分类Dev

缩小APK时Amplify.Auth.fetchAuthSession()userPoolTokens为null

来自分类Dev

即使用户已登录,Auth :: user()也将返回Null

来自分类Dev

auth()->user() 更新用户后返回 null

来自分类Dev

PHP:Mockery 模拟变量 $user = Auth::user()

Related 相关文章

  1. 1

    在Laravel 5.2中auth()-> user()为null

  2. 2

    Auth :: user()返回null

  3. 3

    $ auth为空的问题

  4. 4

    Laravel Auth :: user()关系

  5. 5

    Auth :: user()在Laravel 5.2中返回null

  6. 6

    NOT NULL约束失败:auth_user.password

  7. 7

    Ember ember-simple-auth覆盖routeIfAlreadyAuthenticated在应用程序路由中

  8. 8

    Ember CLI-在路由中注入服务并使用ember-simple-auth获取当前用户

  9. 9

    SQLDecodeError位于/ admin / auth / user /

  10. 10

    Django的auth_user_model

  11. 11

    Laravel 5.3获取尝试为true,但是Auth :: User返回null吗?

  12. 12

    django_auth_ldap custom AUTH_USER_MODEL IntegrityError

  13. 13

    使用哪个Auth :: check()或Auth :: user()-Laravel 5.1?

  14. 14

    找不到类'Illuminate\Foundation\Auth\User' JWT Auth Laravel

  15. 15

    Laravel $auth 调用函数为 $auth->functionname 而不是 $auth->function()

  16. 16

    如何在主路由中加载新路由

  17. 17

    一旦在 HAPI 框架的后续路由中完成身份验证,如何填充 request.auth.isAuthenticated?

  18. 18

    我在Laravel中获得Auth :: user为空

  19. 19

    无法设置设置。在Django中为AUTH_USER_MODEL

  20. 20

    当auth是父代时,Yii user-> checkAccess为false

  21. 21

    我在Laravel中获得Auth :: user为空

  22. 22

    Web和Api路由的Laravel Auth问题

  23. 23

    登录自定义路由被Auth拒绝

  24. 24

    如何创建类似于 Auth::routes() 的路由

  25. 25

    Angular Auth Guard 安全路由

  26. 26

    缩小APK时Amplify.Auth.fetchAuthSession()userPoolTokens为null

  27. 27

    即使用户已登录,Auth :: user()也将返回Null

  28. 28

    auth()->user() 更新用户后返回 null

  29. 29

    PHP:Mockery 模拟变量 $user = Auth::user()

热门标签

归档