Laravel中Pusher触发报错:第一个参数需为对象或已存在类名
Laravel + Pusher 触发错误:"First parameter must either be an object or the name of an existing class" 解决方案
错误详情
调用Pusher->trigger方法时触发以下错误:
First parameter must either be an object or the name of an existing class
调用栈及错误追踪:
Pusher\Pusher->trigger(Array, 'App\\Events\\NewC...', Array, Array)
[2023-09-03 23:39:53] production.ERROR: First parameter must either be an object or the name of an existing class {"exception":"[object] (ErrorException(code: 0): First parameter must either be an object or the name of an existing class at /var/www/wooloveapp.com/vendor/pusher/pusher-php-server/src/Pusher.php:538) [stacktrace] #0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'First parameter...', '/var/www/woolov...', 538, Array) #1 /var/www/wooloveapp.com/vendor/pusher/pusher-php-server/src/Pusher.php(538): property_exists(NULL, 'channels') #2 /var/www/wooloveapp.com/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php(125): Pusher\Pusher->trigger(Array, 'App\\Events\\NewC...', Array, Array) #3 /var/www/wooloveapp.com/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastEvent.php(77): Illuminate\Broadcasting\Broadcasters\PusherBroadcaster->broadcast(Array, 'App\\Events\\NewC...', Array) #4 /var/www/wooloveapp.com/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\Broadcasting\BroadcastEvent->handle(Object(Illuminate\Broadcasting\BroadcastManager)) #5 /var/www/wooloveapp.com/vendor/laravel/framework/src/Illuminate/Container/Util.php(40): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}() #6 /var/www/wooloveapp.com/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure)) #7 /var/www/wooloveapp.com/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure)) #8 /var/www/wooloveapp.com/vendor/laravel/framework/src/Illuminate/Container/Container.php(653): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL) #9 /var/www/wooloveapp.com/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(128): Illuminate\Container\Container->call(Array) #10 /var/www/wooloveapp.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}(Object(Illuminate\Broadcasting\BroadcastEvent)) #11 /var/www/wooloveapp.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Broadcasting\BroadcastEvent)) #12 /var/www/wooloveapp.com/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(132): Illuminate\Pipeline\Pipeline->then(Object(Closure)) #13 /var/www/wooloveapp.com/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php(119): Illuminate\Bus\Dispatcher->dispatchNow(Object(Illuminate\Broadcasting\BroadcastEvent)) #14 /var/www/wooloveapp.com/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(320): Illuminate\Broadcasting\BroadcastManager->queue(Object(App\Events\NewChatEvent)) #15 /var/www/wooloveapp.com/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(243): Illuminate\Events\Dispatcher->broadcastEvent(Object(App\Events\NewChatEvent)) #16 /var/www/wooloveapp.com/vendor/laravel/framework/src/Illuminate/Broadcasting/PendingBroadcast.php(72): Illuminate\Events\Dispatcher->dispatch('App\\Events\\NewC...') #17 /var/www/wooloveapp.com/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(218): Illuminate\Broadcasting\PendingBroadcast->__destruct() #18 /var/www/wooloveapp.com/app/Notifications/NewChatNotif.php(57): broadcast(Object(App\Events\NewChatEvent)) #19 /var/www/wooloveapp.com/app/Notifications/NewChatNotif.php(29): App\Notifications\NewChatNotif->load()
问题根源
从调用栈可见,错误触发于Pusher.php第538行的property_exists(NULL, 'channels'),说明传递给trigger方法的第一个参数(频道相关数据)最终解析为NULL,不符合Pusher SDK的参数要求。
解决方案
检查事件类的
broadcastOn方法:确保App\Events\NewChatEvent中的broadcastOn方法返回有效频道实例或字符串数组,示例:public function broadcastOn() { // 返回私有频道 return new PrivateChannel('chat.' . $this->chat->id); // 或返回多个频道数组 // return [new PrivateChannel('chat.'.$this->chat->id), 'public-channel']; }避免返回
NULL、空数组,或依赖不存在的模型数据生成频道。验证事件实例参数:在
NewChatNotif.php中创建NewChatEvent实例时,确保传入的模型(如$chat)存在且有效,避免传递NULL值导致频道生成失败。检查版本兼容性:确认Laravel版本与Pusher PHP SDK版本匹配,可通过
composer show pusher/pusher-php-server查看版本,参考Laravel官方文档调整至兼容版本。核对广播配置:确认
.env文件中Pusher配置项正确:BROADCAST_DRIVER=pusher PUSHER_APP_ID=your-app-id PUSHER_APP_KEY=your-app-key PUSHER_APP_SECRET=your-app-secret PUSHER_APP_CLUSTER=your-cluster
内容的提问来源于stack exchange,提问作者gabogabans
相关产品推荐
相关产品推荐

