2016-11-14 66 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正在使用多個文件夾結構控制器。提前建議welcomes.thanks ..

回答

0

嘗試這個管腔。

\Illuminate\Contracts\Event\Dispatcher in the controller