Laravel 4

时间:2015-11-10 17:15:50

标签: laravel laravel-4

我正在通过重构和使用事件来清理我的长控制器方法。

不幸的是,我不确定我在何处或如何注册活动订阅者以激活我的活动。

我找到了两个非常有用的教程vegbitCulttt,但我仍然感到困惑。

我已经设置了专门的EmailEventHandler类

namespace Vault\Events;

use Vault\Repository\IndividualEmailRepositoryInterface as IndividualEmailRepositoryInterface;

    protected $individualEmail;

public function __construct(IndividualEmailRepositoryInterface $individualEmail)
  {
    $this->individualEmail = $individualEmail;
  }

Event::subscribe('EmailEventHandler');

class EmailEventHandler {


  public function subscribe($events)
    {
     $events->listen('email.saveIndividual', 'EmailEventHandler@saveIndividual');
    }

  public function saveIndividual($events)
    {
      $save = $this->individualEmail->store($events);
    }
}

完成后,我以为我可以使用Event :: fire访问事件类。这是我的控制器功能:

use Vault\Services\Mailers\UserMailer;

class ContractorController extends BaseController {

protected $mailer

    public function __construct(UserMailer $mailer)
    {
        $this->mailer = $mailer
    }

    public function postIndividualEmail(){

    $file = Input::file('file');
    $subject = Input::get('subject');
    $message = Input::get('message');
    $memberString = Input::get('contractorName');

    //use mail service to send email
    $upload = $this->mail->sendIndividualEmail($memberStringer, $subject, $message, $file);
        if($upload['emailCount'] >= 1){

            //if mail has been sent, fire event
            Event::fire('email.saveIndividual', $upload);

        }

 ...the rest of code

    return Response::json($this->arrResponse);      

}
 }

任何人都能看到我失踪的东西? TIA

1 个答案:

答案 0 :(得分:0)

我首先想到的是你必须在__construct函数中订阅你的活动,如下所示:

public function __construct(IndividualEmailRepositoryInterface $individualEmail)
  {
    $this->individualEmail = $individualEmail;
    Event::subscribe('EmailEventHandler');
  }