dongxiji0687 2016-11-15 10:40
浏览 177
已采纳

在laravel中进行用户身份验证后,我可以在哪里动态更改配置?

I am using elfinder laravel package to manage and organize files.

It has a elfinder.dir config options that used to specifies a directory that user can upload files to it.

Now I want to change (or create) this option to a directory same name as logged in User username.

For that I wrote some codes in a middleware that runs after user authentication for limit access of user to admin panels. like this :

class IsAdmin
    {

        public function handle ($request, Closure $next)
        {
            if (Auth::check()) {

                $username = Auth::user()->username;

                if (!File::exists(public_path('upload') . '/' . $username)) {
                    File::makeDirectory(public_path('upload') . '/' . $username, 0775);
                }
                Config::set('elfinder.dir', ["upload/$username"]);

                return $next($request);
            }
            return Redirect::to('/admin/login');
        }
    }

As you can see if there no directory same as username it will be create.

But I want to know it is right that I do this operations in a middleware or there are another(or proper) place to that ?

  • 写回答

2条回答 默认 最新

  • doubaomao9304 2016-11-16 08:38
    关注

    According to @yannis-berrouag answer and authentication events in l5.3 docs I do this.

    First I added this to EventServiceProvider.php :

    'Illuminate\Auth\Events\Authenticated' => [
            'App\Listeners\SetElfinderConfigs',
    ],
    

    Then I added my desired actions to SetElfinderConfigs listener like this :

    class SetElfinderConfigs
        {
            /**
             * Create the event listener.
             *
             * @return void
             */
            public function __construct ()
            {
                //
            }
    
            /**
             * Handle the event.
             *
             * @param  Authenticated $event
             *
             * @return void
             */
            public function handle (Authenticated $event)
            {
                $username = $event->user->username;
                if (!File::exists(public_path('upload') . '/' . $username)) {
                    File::makeDirectory(public_path('upload') . '/' . $username, 0775);
                }
                Config::set('elfinder.dir', ["upload/$username"]);
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样