如何覆盖推送器配置值?

问题描述

对于我的Web应用程序的特定部分,我需要使用其他Pusher帐户,因此我尝试使用以下方法覆盖配置:

   public function send(Request $request)
    {
        $general_pusher = config('broadcasting.connections.pusher');
        $message = Message::create([
            'from' => auth()->id(),'to' => $request->contact_id,'text' => $request->text
        ]);
        \Config::set('broadcasting.connections.pusher',config('cfg.internal_chat_pusher'));
        logger(config('broadcasting.connections.pusher'));
        broadcast(new NewMessage($message));
        \Config::set('broadcasting.connections.pusher',config('cfg.internal_chat_pusher'));
        return response()->json($message);
    }

然后,我尝试使用Laravel应该引发错误随机值进行测试,但没有错误,并且使用原始/旧的pusher配置值来发送和接收消息。

如您所见,我使用了logger指令,该指令为我提供以下内容

[2020-09-19 14:06:09] local.DEBUG: array (
  'driver' => 'pusher','key' => '1','secret' => '1','app_id' => '1','options' => 
  array (
    'cluster' => '1','useTLS' => true,),)  

但是,即使logger命令输出了应该给出错误的值,我也没有错误,并且消息得到了完美的发送和接收。

如何为特定事件使用不同的推送者帐户?

更新

我尝试如下编辑brodcasting.PHP

 'connections' => [

        'pusher' => [
            'driver' => 'pusher','key' => env('PUSHER_APP_KEY'),'secret' => env('PUSHER_APP_SECRET'),'app_id' => env('PUSHER_APP_ID'),'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),],'redis' => [
            'driver' => 'redis','connection' => 'default','log' => [
            'driver' => 'log','null' => [
            'driver' => 'null','internal_chat_pusher' => [
            'driver' => 'pusher','key' => env('INTERNAL_CHAT_PUSHER_APP_KEY'),'secret' => env('INTERNAL_CHAT_PUSHER_APP_SECRET'),'app_id' => env('INTERNAL_CHAT_PUSHER_APP_ID'),'options' => [
                'cluster' => env('INTERNAL_CHAT_PUSHER_APP_CLUSTER','ap2'),]
            ],];

然后在控制器内部的send函数中:

 public function send(Request $request)
    {
        $message = Message::create([
            'from' => auth()->id(),'text' => $request->text
        ]);
        broadcast::driver('internal_chat_pusher');
        broadcast(new NewMessage($message));
        return response()->json($message);
    }
}

但是,仍然使用原始/旧帐户配置发送邮件

解决方法

这两个选项都应该起作用,但是在 brodcasting.php 中使用单独的驱动程序是一种更清洁的方法。

您的代码正确,但是在命令行中,您还必须记住:

php artisan queue:restart

这将考虑新代码。我自己为此被烧了十几次甚至更多次。