2017-10-19 109 views
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?"; 
     } 
    } 
?> 

回答

0

假設你已經配置Redis如果查不出來的laravel documentationlumen documentation

要發佈消息的引導你ld使用命令

public function handle(Event1 $event) { 
    Redis::publish('channel1', json_encode(['foo' => 'bar'])); 
} 
+0

當Redis :: publish()被調用時出現錯誤。在我的composer.json中我有''照亮/ redis「:」〜5.1「'和''predis/predis」:「〜1.0」',並且在偵聽器文件中也調用了'Use Redis'。供應商文件夾包含illuminate/redis目錄,並在我的bootstrap/app.php文件中註冊了Illuminate \ Redis \ RedisServiceProvider。 – LJP

+0

請分享你的'app.php'文件,並試過'composer dump-autoload'? – linktoahref

+0

'$ app-> configure('broadcasting'); $ app-> register(App \ Providers \ EventServiceProvider :: class); $ app-> register(Illuminate \ Redis \ RedisServiceProvider :: class);' – LJP