我最近将Laravel从5.0更新到5.2,但事件处理似乎发生了一些变化。
当我在监听器中使用变量时$event我不断收到这个令人讨厌的错误。这是有罪的代码:
<?php
namespace AppListeners;
use AppEventsUserWasCreated;
use IlluminateContractsMailMailQueue;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
use Lang;
class UserCreated implements ShouldQueue
{
/**
* @var Mailer
*/
private $mailer;
/**
* Create the event listener.
*
* @param Mailer $mailer
*/
public function __construct( MailQueue $mailer )
{
$this->mailer = $mailer;
}
/**
* Handle the event.
*
* @param UserWasCreated $event
* @return void
*/
public function handle( UserWasCreated $event )
{
$data = array('password' => $event->user->password, 'username' => $event->user->username, 'email' => $event->user->email);
$this->mailer->queue('emails.user_created', $data, function($message) use ($data)
{
$message->to($data['email'])->subject(Lang::get('notifications.USER_CREATED_EMAIL_SUBJECT', [], $event->user->lang));
});
}
}
以下是错误和堆栈跟踪:
[2016-03-21 00:57:57] local.ERROR: exception 'ErrorException' with message 'Undefined variable: event' in /Users/John/Sites/AppBackEnd/vendor/jeremeamia/SuperClosure/src/SerializableClosure.php(210) : eval()'d code:2
Stack trace:
#0 /Users/John/Sites/AppBackEnd/vendor/jeremeamia/SuperClosure/src/SerializableClosure.php(210) : eval()'d code(2): IlluminateFoundationBootstrapHandleExceptions->handleError(8, 'Undefined varia...', '/Users/John...', 2, Array)
#1 [internal function]: {closure}(Object(IlluminateMailMessage))
#2 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(401): call_user_func(Object(Closure), Object(IlluminateMailMessage))
#3 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(165): IlluminateMailMailer->callMessageBuilder(Object(Closure), Object(IlluminateMailMessage))
#4 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(278): IlluminateMailMailer->send('emails.user_cre...', Array, Object(Closure))
#5 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(129): IlluminateMailMailer->handleQueuedMessage(Object(IlluminateQueueJobsRedisJob), Array)
#6 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Jobs/RedisJob.php(50): IlluminateQueueJobsJob->resolveAndFire(Array)
#7 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(210): IlluminateQueueJobsRedisJob->fire()
#8 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(159): IlluminateQueueWorker->process('redis', Object(IlluminateQueueJobsRedisJob), '0', '0')
#9 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(110): IlluminateQueueWorker->pop('', 'default', '0', '3', '0')
#10 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(72): IlluminateQueueConsoleWorkCommand->runWorker('', 'default', '0', '128', false)
#11 [internal function]: IlluminateQueueConsoleWorkCommand->fire()
#12 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Container/Container.php(507): call_user_func_array(Array, Array)
#13 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Console/Command.php(169): IlluminateContainerContainer->call(Array)
#14 /Users/John/Sites/AppBackEnd/vendor/symfony/console/Command/Command.php(256): IlluminateConsoleCommand->execute(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
#15 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Console/Command.php(155): SymfonyComponentConsoleCommandCommand->run(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
#16 /Users/John/Sites/AppBackEnd/vendor/symfony/console/Application.php(791): IlluminateConsoleCommand->run(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
#17 /Users/John/Sites/AppBackEnd/vendor/symfony/console/Application.php(186): SymfonyComponentConsoleApplication->doRunCommand(Object(IlluminateQueueConsoleWorkCommand), Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
#18 /Users/John/Sites/AppBackEnd/vendor/symfony/console/Application.php(117): SymfonyComponentConsoleApplication->doRun(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
#19 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(107): SymfonyComponentConsoleApplication->run(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
#20 /Users/John/Sites/AppBackEnd/artisan(36): IlluminateFoundationConsoleKernel->handle(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
#21 {main}
尝试将$event传递到闭包中:
$this->mailer->queue('emails.user_created', $data, function($message) use ($data, $event)
编辑,首先尝试设置语言并检查两个值:
// including app instance
use IlluminateFoundationApplication;
public function handle(UserWasCreated $event, Application $app)
{
$lang = $event->user->lang;
$app->setLocale($lang);
$data = array('password' => $event->user->password, 'username' => $event->user->username, 'email' => $event->user->email);
// no need to pass the event, because the locate is already set
$this->mailer->queue('emails.user_created', $data, function($message) use ($data){
$message->to($data['email'])->subject(Lang::get('notifications.USER_CREATED_EMAIL_SUBJECT'));
});
}
你需要
$event传递给闭包
public function handle( UserWasCreated $event )
{
$data = array('password' => $event->user->password, 'username' => $event->user->username, 'email' => $event->user->email);
// Pass event to the closure - use($data, $event)
$this->mailer->queue('emails.user_created', $data, function($message) use ($data, $event)
{
$message->to($data['email'])->subject(Lang::get('notifications.USER_CREATED_EMAIL_SUBJECT', [], $event->user->lang));
});
}
解决方案,如建议:
<?php
namespace AppListeners;
use AppEventsUserWasCreated;
use IlluminateContractsMailMailQueue;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationApplication;
use Lang;
class UserCreated implements ShouldQueue
{
/**
* @var Mailer
*/
private $mailer;
private $app;
/**
* Create the event listener.
*
* @param Mailer $mailer
*/
public function __construct( MailQueue $mailer, Application $app )
{
$this->mailer = $mailer;
$this->app = $app;
}
/**
* Handle the event.
*
* @param UserWasCreated $event
* @return void
*/
public function handle( UserWasCreated $event )
{
$lang = $event->user->lang;
$this->app->setLocale($lang);
$data = array('code' => $event->user->email_confirmation_code, 'username' => $event->user->username, 'email' => $event->user->email);
// no need to pass the event, because the locate is already set
$this->mailer->queue('emails.user_created', $data, function($message) use ($data)
{
$message->to($data['email'])->subject(Lang::get('notifications.USER_CREATED_EMAIL_SUBJECT'));
});
}
}