0
我在我的流明應用程序中有一個名爲'Event1'的事件和一個事件監聽器'Event1Listener'。當事件1被觸發時,我需要將自定義消息發佈到名爲'channel1'的redis頻道。我怎樣才能做到這一點?如何使用流明廣播將消息發佈到redis頻道?
Event1.php
<?php
namespace App\Events;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class Event1 extends Event implements ShouldBroadcast
{
/**
* Create a new event instance.
* @return void
*/
public function __construct() {
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn() {
return ['channel1'];
}
}
?>
Event1Listener.php
<?php
namespace App\Listeners;
use App\Events\Event1;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class Event1Listener {
/**
* Create the event listener.
*
* @return void
*/
public function __construct(){
}
/**
* Handle the event.
*
* @param Event1 $event
* @return void
*/
public function handle(Event1 $event) {
echo "What should I add here?";
}
}
?>
當Redis :: publish()被調用時出現錯誤。在我的composer.json中我有''照亮/ redis「:」〜5.1「'和''predis/predis」:「〜1.0」',並且在偵聽器文件中也調用了'Use Redis'。供應商文件夾包含illuminate/redis目錄,並在我的bootstrap/app.php文件中註冊了Illuminate \ Redis \ RedisServiceProvider。 – LJP
請分享你的'app.php'文件,並試過'composer dump-autoload'? – linktoahref
'$ app-> configure('broadcasting'); $ app-> register(App \ Providers \ EventServiceProvider :: class); $ app-> register(Illuminate \ Redis \ RedisServiceProvider :: class);' – LJP