2016-11-14 1 views
0

luman의 새 기능 laravel. luman laravel의 이벤트를 호출하는 방법은 무엇입니까? 이벤트를 luman laravel에서 리스너에게 호출하는 방법은 무엇입니까?

내 이벤트 파일 : SendMail.php

<?php 
namespace App\Events; 
use App\Events\Event; 
use Illuminate\Queue\SerializesModels; 
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; 

class SendMail extends Event 
{ 
    use SerializesModels; 
    public $userId; 
    public function __construct($userId) 
    { 

       $this->userId = $userId; 

    } 
    public function broadcastOn() 
    { 
     return []; 
    } 
} 

내 리스너 파일 : SendMailFired.php

<?php 
namespace App\Listeners; 

use App\Events\SendMail; 
use Illuminate\Queue\InteractsWithQueue; 
use Illuminate\Contracts\Queue\ShouldQueue; 

use Event; 
class SendMailFired 
{ 

    public function __construct() 
    { 



    } 
    public function handle(SendMail $event) 
    { 


       $to = "[email protected]"; 
      $subject = "HTML email"; 

      $message = " 
      <html> 
      <head> 
      <title>HTML email</title> 
      </head> 
      <body> 
      <p>This email contains HTML Tags!</p> 
      <table> 
      <tr> 
      <th>Firstname</th> 
      <th>Lastname</th> 
      </tr> 
      <tr> 
      <td>John</td> 
      <td>Doe</td> 
      </tr> 
      </table> 
      </body> 
      </html> 
      "; 

      // Always set content-type when sending HTML email 
      $headers = "MIME-Version: 1.0" . "\r\n"; 
      $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 

      // More headers 
      $headers .= 'From: <[email protected]>' . "\r\n"; 
      $headers .= 'Cc: [email protected]' . "\r\n"; 
      if(mail($to,$subject,$message,$headers)){ 
       return "success"; 
      }else{ 
       return "fail"; 
      } 

     } 
    } 

내 이벤트 서비스 제공 파일 :

<?php 

namespace App\Providers; 
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; 
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; 
class EventServiceProvider extends ServiceProvider 
{ 
    protected $listen = [ 

     'App\Events\SendMail' => [ 
      'App\Listeners\SendMailFired', 
     ], 
    ]; 
    public function boot(DispatcherContract $events) 
    { 


     parent::boot($events); 
    } 
} 

내 컨트롤러 파일 : Emailtestcontroller .php

<?php 
namespace App\Http\Controllers\Email; 
use App\Http\Requests; 
use Illuminate\Http\Request; 
use Event; 
use App\Events\SendMail; 
use App\Http\Controllers\Controller; 

use Illuminate\Events\Dispatcher; 



class EmailControllertest extends Controller 
{ 
    public function __construct() 
    { 
     //$this->middleware('auth'); 
    } 
    public function exam() 
    { 
     Event::fire(new SendMail(2)); 
     return view('home'); 


    } 
} 

내 경로 파일 :

$app->post('/email', 'Email\[email protected]'); 

모르겠어요이 올바른 방법 또는 not.even 컨트롤러에 대해 여러 폴더 구조를 사용하고있다. 제안을 환영합니다. 미리 감사드립니다.

답변

0

시도해주세요.

\Illuminate\Contracts\Event\Dispatcher in the controller 
관련 문제