Ошибка обработки событий Laravel 5.2

Я недавно обновил Laravel с 5.0 до 5.2, но кажется, что обработка событий немного изменилась.

Я продолжаю получать эту неприятную ошибку, когда использую переменную $ event в моем Listener.
Вот инкриминированный код:

<?php

namespace App\Listeners;

use App\Events\UserWasCreated;

use Illuminate\Contracts\Mail\MailQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

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): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined varia...', '/Users/John...', 2, Array)
#1 [internal function]: {closure}(Object(Illuminate\Mail\Message))
#2 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(401): call_user_func(Object(Closure), Object(Illuminate\Mail\Message))
#3 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(165): Illuminate\Mail\Mailer->callMessageBuilder(Object(Closure), Object(Illuminate\Mail\Message))
#4 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(278): Illuminate\Mail\Mailer->send('emails.user_cre...', Array, Object(Closure))
#5 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(129): Illuminate\Mail\Mailer->handleQueuedMessage(Object(Illuminate\Queue\Jobs\RedisJob), Array)
#6 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Jobs/RedisJob.php(50): Illuminate\Queue\Jobs\Job->resolveAndFire(Array)
#7 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(210): Illuminate\Queue\Jobs\RedisJob->fire()
#8 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(159): Illuminate\Queue\Worker->process('redis', Object(Illuminate\Queue\Jobs\RedisJob), '0', '0')
#9 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(110): Illuminate\Queue\Worker->pop('', 'default', '0', '3', '0')
#10 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(72): Illuminate\Queue\Console\WorkCommand->runWorker('', 'default', '0', '128', false)
#11 [internal function]: Illuminate\Queue\Console\WorkCommand->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): Illuminate\Container\Container->call(Array)
#14 /Users/John/Sites/AppBackEnd/vendor/symfony/console/Command/Command.php(256): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#15 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Console/Command.php(155): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#16 /Users/John/Sites/AppBackEnd/vendor/symfony/console/Application.php(791): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#17 /Users/John/Sites/AppBackEnd/vendor/symfony/console/Application.php(186): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#18 /Users/John/Sites/AppBackEnd/vendor/symfony/console/Application.php(117): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#19 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(107): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#20 /Users/John/Sites/AppBackEnd/artisan(36): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#21 {main}

2

Решение

Вам нужно передать $ событие на закрытие

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));
});
}
1

Другие решения

Попробуйте передать событие $ в замыкание:

$this->mailer->queue('emails.user_created', $data, function($message) use ($data, $event)

редактировать, попробуйте сначала установить язык и проверьте оба значения:

// including app instance
use Illuminate\Foundation\Application;

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'));
});
}
1

Решение, как предлагается:

<?php

namespace App\Listeners;

use App\Events\UserWasCreated;

use Illuminate\Contracts\Mail\MailQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Application;

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'));
});

}
}
0
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector