【问题标题】:laravel broadcasting for multiple guardlaravel 广播多后卫
【发布时间】:2018-12-03 01:30:28
【问题描述】:

我有以下为我的应用程序定义的身份验证保护adminsdesignerscustomers 等。默认保护是designer guard

我希望每个guard 都有自己的private channel。 所以我在我的 channel.php 中定义它,每个条目都有多个条目,如下所示

Broadcast::channel('private.admins.{id}', function ($admin, $id) {


    Log::info($admin);
    //logging the admin

});

但这始终是 bindingdefault guard 类所以我的问题是我如何告诉它在这里使用 Admin model。 我无法在任何地方找到它。那么你能指出我正确的方向吗

其实我希望每个guard 都有自己的private channel

【问题讨论】:

    标签: php laravel socket.io broadcast


    【解决方案1】:

    尝试更改BroadcastServiceProvider文件app\Providers\BroadcastServiceProvider.php

    每个守卫的广播认证端点不同

    public function boot()
    {
       //Broadcast::routes();
       //match any of the 3 auth guards
       Broadcast::routes(['middleware' => ['web','auth:admins,designers,customers']]);
       require base_path('routes/channels.php');
    }
    

    现在在 channels.php 中

    Broadcast::channel('admins.channel.{id}', function ($model, $id) {
          return $model->id === $id && get_class($model) === 'App\Admin';
    });
    
    Broadcast::channel('designers.channel.{id}', function ($model, $id) {
          return $model->id === $id && get_class($model) === 'App\Designer';
    });
    
    Broadcast::channel('customers.channel.{id}', function ($model, $id) {
          return $model->id === $id && get_class($model) === 'App\Customer';
    });
    

    【讨论】:

    • 我先添加 Broadcast::routes([ "middleware" => ["web","auth:admin"] ]); 并且管理员防护工作完美,但是当我添加前缀时,laravel-echo-server 出现错误 404
    • 不要加前缀,我已经更新了答案,你可以试试第二个
    • 使用了这个:Broadcast::routes([ "middleware" => ["web","auth:admin, designer, customer"] ]); 现在没有定义守卫管理员发生的错误 500 !!!!!!
    • 其实我定义了管理员守卫
    • 你必须使用基于前缀的。你的广播授权网址是什么?
    【解决方案2】:

    我正在发布这个答案,给每一个可能会面临这个问题的人。我正在使用 laravel 7 和 beyondcode/laravel-websockets。当我在 BoradcastServiceProvider.php 中指定 midllewares 的源代码中挖掘将不起作用。为通道定义保护的唯一方法是为通道指定选项:

     Broadcast::channel('messaging.organ.{id}', function ($organ , $id) {
        return $organ->id == $id && get_class($organ) === "App\Organization";
    } , ['guards' => ['organ']]);
    

    原因: 因为我使用的是beyondcode/laravel-websockets,所以我在src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php 中挖掘,在这个文件中retrieveUser 方法将获得用户。在此文件中,如果提供了频道选项,则将返回指定警卫中的用户。您可以定义一个或多个守卫,但它只会返回一个作为阵列中第一个守卫登录的用户。

        protected function retrieveUser($request, $channel)
    {
        $options = $this->retrieveChannelOptions($channel);
    
        $guards = $options['guards'] ?? null;
    
        if (is_null($guards)) {
            return $request->user();
        }
    
        foreach (Arr::wrap($guards) as $guard) {
            if ($user = $request->user($guard)) {
                return $user;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-07
      • 2018-04-13
      • 2019-05-16
      • 2023-03-04
      • 2021-05-20
      • 2017-12-26
      • 2018-01-06
      相关资源
      最近更新 更多