我的 lumen 应用程序中有一个名为“Event1”的事件和一个事件监听器“Event1Listener”。当 event1 被触发时,我需要将自定义消息发布到名为“channel1”的 redis channel 。我该怎么做?
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
如果没有,请查看 laravel documentation和 lumen documentation
要将消息发布到 channel ,您可以使用命令
public function handle(Event1 $event) {
Redis::publish('channel1', json_encode(['foo' => 'bar']));
}
关于php - 如何使用 lumen 广播将消息发布到 redis channel ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46823462/