Laravel 5.2事件未在生产中触发 [英] Laravel 5.2 event not firing in production
问题描述
我有一个laravel事件,该事件在本地运行良好,但在生产环境中却不行.我需要它既可以广播给Redis,也可以广播给laravel侦听器.广播到redis可以正常工作,但是laravel侦听器似乎无法正常工作.
I have a laravel event that works fine locally but not in production. I need it to both broadcast to redis as well as to a laravel listener. Broadcasting to redis works as expected, but the laravel listener seems to not be working.
这是事件的代码:
<?php
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class UserAlert extends Event implements ShouldBroadcast
{
public $email;
public $type;
public $message;
public $data;
use SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($email, $type, $message, $data = [])
{
$this->email = $email;
$this->type = $type;
$this->message = $message;
$this->data = $data;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return ['user-alert'];
}
}
这是侦听器的代码
<?php
namespace App\Listeners;
use App\Events\UserAlert;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Redis;
use Log;
class UserAlertListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
public function subscribe($events)
{
$events->listen(
'App\Events\UserAlert',
'App\Listeners\UserAlertListener@handle'
);
}
/**
* Handle the event.
*
* @param UserAlert $event
* @return void
*/
public function handle(UserAlert $event)
{
$key = $event->email;
$arr[] = $event;
$current = json_decode(Redis::get($key));
if(isset($current)){
$arr = array_merge($arr, $current);
}
Redis::set($key, json_encode($arr));
Redis::expireat($key, strtotime('+1 week'));
}
}
这是我的活动服务提供商:
And here is my Event Service Provider:
<?php
namespace Illuminate\Foundation\Support\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use App\Events\UserAlert;
use App\Listeners\UserAlertListener;
class EventServiceProvider extends ServiceProvider
{
/**
* The event handler mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\UserAlert' => [
'App\Listeners\UserAlertListener'
],
];
/**
* The subscriber classes to register.
*
* @var array
*/
protected $subscribe = [
'App\Listeners\UserAlertListener'
];
/**
* Register the application's event listeners.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return void
*/
public function boot(DispatcherContract $events)
{
foreach ($this->listens() as $event => $listeners) {
foreach ($listeners as $listener) {
$events->listen($event, $listener);
}
}
foreach ($this->subscribe as $subscriber) {
$events->subscribe($subscriber);
}
}
/**
* {@inheritdoc}
*/
public function register()
{
//
}
/**
* Get the events and handlers.
*
* @return array
*/
public function listens()
{
return $this->listen;
}
}
我尝试在没有运气的情况下运行composer dumpautoload
和php artisan clear-compiled
.
I've tried running composer dumpautoload
and php artisan clear-compiled
with no luck.
我尝试将dd('test')添加到我的侦听器中以查看它是否被调用.不是.
I've tried adding dd('test') to my listener to see if it is getting called. It's not.
我确实有一个使用Redis和Supervisor运行的队列
I do have a queue running using redis and supervisor
推荐答案
尝试:
$ php artisan clear-compiled
$ php artisan optimize
$ composer install
$ composer dumpautoload
$ php artisan cache:clear
还要确保在eventServiceProvider
中一切正常.如果您使用排队作业,请查看Kevin Kuiper的答案.
Also make sure all is ok in your eventServiceProvider
. If you use Queued job, check Kevin Kuiper answer.
与您相同,并且具有相同的行为:事件被触发,但是监听器从未到达.这些命令后按预期工作.还使用了一些dd()
来测试监听器.
Did same as you and had same behavior: event fired, but listener never reached. Worked as expected after those commands. Also used some dd()
to test listener.
来源:此和我已经使用的命令的混合
Source: mix of this and command I already used.
这篇关于Laravel 5.2事件未在生产中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!