简体   繁体   English

Laravel 5.6共享身份验证ID问题

[英]Laravel 5.6 share auth id issue

I want to pass user id in the boot function of the AppServiceProvider class (as it written in the docs ) but for some reason Auth::user() object is empty. 我想在AppServiceProvider类的boot功能中传递用户ID(如docs中所写),但由于某种原因Auth::user()对象为空。

myAppName/app/Providers/AppServiceProvider.php myAppName /应用程序/提供者/ AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\URL;

use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Auth;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
        URL::forceScheme('https');
        View::share('user_info', Auth::user());
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

And then in in some blade template: 然后在一些刀片模板中:

<p>{{ $user_info->id }}</p>

As a result an error occurs: 结果发生错误:

Trying to get property 'id' of non-object

There is same error if want to pass Auth::user()->id 如果要通过Auth::user()->id也会出现相同的错误

How to make it work? 如何使其运作?

Laravel blade has access to the currently authenticated user, you don't have to explicitly use view::share for that. Laravel刀片服务器可以访问当前经过身份验证的用户,因此您不必显式使用view :: share。

You can simply access auth user in the blade as : 您可以简单地通过以下方式访问刀片服务器中的auth用户:

{{ Auth::user()->id }}

You might check if user already authenticated via @guest helper or auth check method: 您可以通过@guest helper或auth check方法检查用户是否已通过身份验证:

@if (Auth::check())
  // authenticated
  {{ Auth::user()->id }}
@else
  // Not authenticaed
@endif

for security reason Laravel ServiceProvider doesn't have any access to session. 出于安全原因,Laravel ServiceProvider无权访问会话。 And After login laravel maintain current login user into Session. 登录后laravel保持当前登录用户进入Session。 That's why you are getting this error. 这就是为什么您会收到此错误。

For your problem you can share variable into Controller(App/Http/Controllers/Controller.php) class's constructor. 对于您的问题,您可以将变量共享到Controller(App / Http / Controllers / Controller.php)类的构造函数中。

Something like this: 像这样:

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function __construct()
    {
        view()->share('user',auth()->user());
    }
}

All controllers in Laravel default extends this class. Laravel中的所有控制器默认都扩展了此类。 Then you just have to execute constructor of parent class like this: 然后,您只需要执行父类的构造函数,如下所示:

class HomeController extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }
}

One more thing default session is not sharable into Constructor also. 默认会话也不能再共享到Constructor中。 You have to enable it by adding this two lines into App/Http/Kernel.php class's $middleware array. 您必须通过将这两行添加到App / Http / Kernel.php类的$ middleware数组中来启用它。

class Kernel extends HttpKernel
{
    protected $middleware = [
       //
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
       //
    ];
}

Make sure you are removing this two lines from $middlewareGroups array. 确保从$ middlewareGroups数组中删除了这两行。 Good luck !!! 祝好运 !!!

This is a solution, just need to add View Composer to the AppServiceProvider 这是一个解决方案,只需将View Composer添加到AppServiceProvider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\URL;

use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Auth;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        view()->composer('layouts.navbar', function($view) {

           $view->with('user_id', Auth::user()->id);

        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Then just put a variable in the navbar template like this: <p>{{ $user_id }}</p> 然后只需将一个变量放在navbar模板中,如下所示: <p>{{ $user_id }}</p>

Explanation here 此处说明

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

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