2016-03-21 77 views
2

我最近更新Laravel從5.0到5.2,但它似乎事件處理已經改變了一下。Laravel 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} 

回答

1

您需要$事件傳遞給封閉

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

嘗試,但沒有成功 – Washery

+1

爲什麼你逝去'$事件'作爲Lang :: get的第三個參數? – manix

+0

根據用戶設置選擇正確的語言 – Washery

0

的解決方案,建議:

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

    } 
}