简体   繁体   English

Laravel - 如何在 AppServiceProvider 中获取当前用户

[英]Laravel - How to get current user in AppServiceProvider

So I am usually getting the current user using Auth::user() and when I am determining if a user is actually logged in Auth::check .所以我通常使用Auth::user()获取当前用户,当我确定用户是否实际登录Auth::check However this doesn't seem to work in my AppServiceProvider .但是,这在我的AppServiceProvider似乎不起作用。 I am using it to share data across all views.我正在使用它在所有视图中共享数据。 I var_dump both Auth::user() and Auth::check() while logged in and I get NULL and false .我在登录时同时对Auth::user()Auth::check()进行var_dump ,我得到NULLfalse

How can I get the current user inside my AppServiceProvider ?如何在AppServiceProvider获取当前用户? If that isn't possible, what is the way to get data that is unique for each user (data that is different according to the user_id ) across all views.如果这是不可能的,那么在所有视图中获取每个用户唯一的数据(根据user_id不同的数据)的方法是什么。 Here is my code for clarification.这是我的澄清代码。

if (Auth::check()) {
        $cart = Cart::where('user_id', Auth::user()->id);
        if ($cart) {
            view()->share('cart', $cart);

        }
    } else {
        view()->share('cartItems', Session::get('items'));
    }

Laravel session is initialized in a middleware so you can't access the session from a Service Provider, because they execute before the middleware in the request lifecycle Laravel 会话在中间件中初始化,因此您无法从服务提供者访问会话,因为它们在请求生命周期中的中间件之前执行

You should use a middleware to share your varibles from the session您应该使用中间件来共享会话中的变量

If for some other reason you want to do it in a service provider, you could use a view composer with a callback, like this:如果由于其他原因您想在服务提供者中执行此操作,您可以使用带有回调的视图编辑器,如下所示:

public function boot()
{
    //compose all the views....
    view()->composer('*', function ($view) 
    {
        $cart = Cart::where('user_id', Auth::user()->id);

        //...with this variable
        $view->with('cart', $cart );    
    });  
}

The callback will be executed only when the view is actually being composed, so middlewares will be already executed and session will be available只有在实际组合视图时才会执行回调,因此中间件将已经执行并且会话将可用

In AuthServiceProvider 's boot() function write these lines of codeAuthServiceProviderboot()函数中编写这些代码行

public function boot()
{
    view()->composer('*', function($view)
    {
        if (Auth::check()) {
            $view->with('currentUser', Auth::user());
        }else {
            $view->with('currentUser', null);
        }
    });
}

Here * means - in all of your views $currentUser variable is available.这里*表示 - 在您的所有视图中$currentUser变量都可用。

Then, from view file {{ currentUser }} will give you the User info if user is authenticated otherwise null.然后,如果用户已通过身份验证,则从视图文件{{ currentUser }}将为您提供用户信息,否则为空。

As per my Laravel 7 experience, Auth::user() does not work in AppServiceProvider.php instead the helper function auth()->user() did the trick for me.根据我的 Laravel 7 经验, Auth::user()AppServiceProvider.php不起作用,而是辅助函数auth()->user()为我解决了问题。

public function boot()
    {

        if (auth()->check()) {
            View::composer('layout.app', function ($view) {
                $userName = auth()->user()->name;
                $view->with(['userName' => $userName]);
            });
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM