在AppServiceProvider中使用Auth [英] Use Auth in AppServiceProvider

查看:55
本文介绍了在AppServiceProvider中使用Auth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need the ID of the user who is logged in to get a photo in the profile table, here I am trying to use View but only in the index function that gets $profile, I want all files in the view to have $profile

public function index(){
  $profil = Profil_user::where('user_id',$auth)->first();
  View::share('profil', $profil);
  return view('user.index');
}

我也尝试了AppServiceProvider,但是如果我未登录,则会收到空值形式的错误,是否可以解决我的问题?

I have also tried AppServiceProvider but I get an error in the form of a null value if I don't log in, is there a solution to my problem?

public function boot(){
    $auth = Auth::user();
    dd($auth);
}

推荐答案

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

exist several way to pass a variable to all views. I explain some ways.

1..对于需要将变量传递给这些路由的所有路由,请使用中间件:

1. use middleware for all routes that you need to pass variable to those:

create middleware (I named it RootMiddleware)

php artisan make:middleware RootMiddleware

转到 app/Http/Middleware/RootMiddleware.php 并执行以下示例代码:

go to app/Http/Middleware/RootMiddleware.php and do following example code:

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 数组.

then must register this middleware in app/Http/Kernel.php and put this line 'root' => RootMiddleware::class, to protected $routeMiddleware array.

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

then use this middleware of routes or routes group, for example:

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..使用视图编辑器将变量传递给所有视图的其他方式转到 app/Http 并创建Composers文件夹,并在其中创建 ProfileComposer.php ,在 ProfileComposer.php 内部,如下所示:


2. other way that you pass variable to all views with view composer go to app/Http and create Composers folder and inside it create ProfileComposer.php, inside ProfileComposer.php like this:

<?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
        ]);
    }
}

现在是时候创建您的服务提供商类了,我将其命名为 ComposerServiceProvider 在终端中编写以下命令: php artisan make:provider ComposerServiceProvider 成功获取 Provider创建后.消息转到 config/app.php 并使用以下 \ App \ Providers \ ComposerServiceProvider :: class 到提供者数组.现在转到 app/Providers/ComposerServiceProvider.php 并执行以下操作:

now it's time create your service provider class, I named it ComposerServiceProvider write this command in terminal : php artisan make:provider ComposerServiceProvider after get Provider created successfully. message go to config/app.php and register your provider with put this \App\Providers\ComposerServiceProvider::class to providers array. now go to app/Providers/ComposerServiceProvider.php and do like following:

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()
    {
    }
}


3.,可能是在没有创建服务提供商的情况下在 AppServiceProvider 中共享您的变量,请转到 app/Provider/AppServiceProvider.php 请执行以下操作:


3. is possible that without create a service provider share your variable in AppServiceProvider, go to app/Provider/AppServiceProvider.php and do as follows:

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

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

我希望会有用

这篇关于在AppServiceProvider中使用Auth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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