如何在luman laravel中向侦听器调用事件?

时间:2016-11-14 12:41:02

标签: php web-services 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 = "sakthiyendran.chandran@mercuryminds.com";
            $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: <mail2raj7777@gmail.com>' . "\r\n";
            $headers .= 'Cc: felixrajesh006@gmail.com' . "\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\EmailControllertest@exam');

我不知道这是否正确。即使是使用多个文件夹结构的控制器。

1 个答案:

答案 0 :(得分:0)

尝试使用流明。

\Illuminate\Contracts\Event\Dispatcher in the controller