Laravel 在AppServiceProvider中使用身份验证

Laravel 在AppServiceProvider中使用身份验证,laravel,laravel-5,Laravel,Laravel 5,我需要登录用户的ID才能在配置文件表中获取照片,这里我尝试使用View,但仅在获取$profile的索引函数中,我希望视图中的所有文件都具有$profile public function index(){ $profil = Profil_user::where('user_id',$auth)->first(); View::share('profil', $profil); return view('user.index'); } 我也尝试过AppServiceProv

我需要登录用户的ID才能在配置文件表中获取照片,这里我尝试使用View,但仅在获取
$profile
的索引函数中,我希望视图中的所有文件都具有
$profile

public function index(){
  $profil = Profil_user::where('user_id',$auth)->first();
  View::share('profil', $profil);
  return view('user.index');
}
我也尝试过AppServiceProvider,但是如果我不登录,我会得到一个空值形式的错误,有解决问题的方法吗

public function boot(){
    $auth = Auth::user();
    dd($auth);
}
你可以用这个

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

存在几种将变量传递给所有视图的方法。我解释了一些方法

1。对需要将变量传递给以下路径的所有路径使用中间件:

create middleware (I named it RootMiddleware)
转到
app/Http/Middleware/RootMiddleware.php
并执行以下示例代码:

public function handle($request, Closure $next) {
    if(auth()->check()) {
        $authUser = auth()->user();
        $profil = Profil_user::where('user_id',$authUser->id)->first();

        view()->share([
            'profil', $profil
        ]);
    }

    return $next($request);
}
然后必须在
app/Http/Kernel.php
中注册此中间件,并将此行
'root'=>RootMiddleware::class,
放在
受保护的$routeMiddleware
数组中

然后使用路由或路由组的中间件,例如:

Route::group(['middleware' => 'root'], function (){
    // your routes that need to $profil, of course it can be used for all routers(because in handle function in RootMiddleware you set if
});
或设置为单根:

Route::get('/profile', 'ProfileController@profile')->name('profile')->middleware('RootMiddleware');

2.使用view composer将变量传递给所有视图的其他方式 转到
app/Http
并创建Composer文件夹,在其中创建
ProfileComposer.php
,在
ProfileComposer.php
中,如下所示:

<?php


namespace App\Http\View\Composers;

use Illuminate\View\View;

class ProfileComposer
{
    public function __construct()
    {

    }

    public function compose(View $view)
    {
        $profil = Profil_user::where('user_id', auth()->id)->first();
        $view->with([
            'profil' => $profil
        ]);
    }
}

3.在不创建服务提供商的情况下,可以在
AppServiceProvider
中共享变量,转到
app/provider/AppServiceProvider.php
并执行以下操作:

// Using class based composers...
View::composer(
    'profile', 'App\Http\View\Composers\ProfileComposer'
);

// Using Closure based composers...
View::composer('dashboard', function ($view) {
      //
});

我希望有用

您想在所有文件中使用$profile吗?
namespace App\Providers;

use App\Http\View\Composers\ProfileComposer;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        View::composer(
            '*' , ProfileComposer::class // is better in your case use write your views that want to send $profil variable to those
        );

       /* for certain some view */

        //View::composer(
        //    ['profile', 'dashboard'] , ProfileComposer::class
        //);


       /* for single view */

        //View::composer(
        //    'app.user.profile' , ProfileComposer::class
        //);

    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
    }
}
// Using class based composers...
View::composer(
    'profile', 'App\Http\View\Composers\ProfileComposer'
);

// Using Closure based composers...
View::composer('dashboard', function ($view) {
      //
});