【问题标题】:Function AFTER authentication and BEFORE view (laravel)功能AFTER authentication and BEFORE view (laravel)
【发布时间】:2021-05-29 13:39:07
【问题描述】:

我正在尝试从数据库中获取设置并将它们放入配置中, 我的功能需要用户 ID,所以它只能带来他的设置, 在服务提供者(启动功能)中还没有身份验证,你能告诉我在正确的地方运行我的功能吗,请注意我需要在视图渲染之前运行它,因为里面有布局的设置,这是我的功能:

// public static becouse it's inside Class//
public static function getAppSettings(){
        if (!config('settings') && Auth::check()) {
            $user_id = Auth::user()->id;
            $settings = AppSettings::where('user_id', $user_id)->get()->all();
            $settings = Cache::remember('settings', 60, function () use ($settings) {
                // Laravel >= 5.2, use 'lists' instead of 'pluck' for Laravel <= 5.1
                return $settings->pluck('value', 'key')->all();
            });
            config()->set('settings', $settings);
        }else{
            // this is for testing//
            dd('no');
        }
    }

没有授权,它可以在服务提供者(启动功能)内部工作,但它会为所有用户带来所有设置。

【问题讨论】:

    标签: php laravel authentication


    【解决方案1】:

    您可以为此创建中间件。路由之后和控制器之前的中间件调用

    php artisan make:middleware Settings
    

    这将创建下面的类

    <?php
    
    namespace App\Http\Middleware;
    
    use Closure;
    use Illuminate\Http\Request;
    
    class Settings
    {
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
        public function handle(Request $request, Closure $next)
        {
          // exicute your logic here
            return $next($request);
        }
    }
    

    你可以在 handle 和 next 之前调用你的方法 你可以阅读更多关于这个 https://laravel.com/docs/8.x/middleware

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 2011-10-23
      • 2014-11-30
      • 2013-01-28
      • 2012-12-21
      相关资源
      最近更新 更多