Laravel 5.2 事件处理错误

Laravel 5.2 event handling error

我最近将 Laravel 从 5.0 更新到 5.2,但似乎事件处理发生了一些变化。

当我在 Listener 中使用 $event 变量时,我不断收到这个令人讨厌的错误。 以下是相关代码:

<?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}  

您需要将 $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));
        });
 }

尝试将 $event 传递到闭包中:

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

解决方案,建议:

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

    }
}