laravelオブザーバモード

PHPどのように多くのデザインパターン?

これは拷問魂のピンチではありません

PHPがある呉のデザインパターンは、私は確信して、あなたのすべての既知の大物
持って戦略パターン、工場出荷時のパターン、シングルトン、登録モード、アダプタモード、オブザーバ・モードを
ここに画像を挿入説明


記事はについて話オブザーバーパターン(オブザーバー)
コンセプト:
ときにオブジェクトの状態が変化し、その扶養家族は、事件後に実行するロジックを作るために、受動的な行動を変更を監視します。オブザーバモデル低いカップリングを達成するために、通知および更新するための非侵入機構

利点:
1.観測されたと観測者との間に結合された抽象モデルを確立オブザーバー。オブザーバーの唯一の特定のリスト、既知の観察者の役割、各観察者が、特定の抽象オブザーバインタフェースを満たしているのです。オブザーバーは、彼らが共通のインタフェース持っていることを知って、任意の特定の観察者の知らない
2としては、しっかりと彼らは異なる抽象化レベルに属することができるように、観測者と観測者に結合されていません。観測者と観測されたが、一緒にスローされた場合、そのオブジェクトは、抽象化と具体的なレベルを越えバインドされている
通信をブロードキャスト3.オブザーバ・モードがサポート。観察者は登録されているすべてのオブザーバーに通知します

laravelのオブザーバーは、次に起こるのでしょうか?

リスナーの追加
にlaravelを\アプリ\プロバイダ\ EventServiceProvider.php フォルダは、独自のリスナーを定義する私たち最初の必要性をイベントリスナーの定義されています

// 新增事件
'App\Events\EmailLogin' => [
// 发送邮箱验证码
      'App\Listeners\SendEmail',
],

次のとおりです。

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'App\Events\Event' => [
            'App\Listeners\EventListener',
        ],

        // 新增事件
        'App\Events\EmailLogin' => [
            // 发送邮箱验证码
            'App\Listeners\SendEmail',
        ],

    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();

        //
    }
}

OF

PHPの職人のイベント:発生

*リスナーのファイルを作成します。
ここに画像を挿入説明


ファイルを変更します。
emaillogin.php

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

use Admins;

class EmailLogin
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    protected $email_binding;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($email_binding)
    {
        $this->email_binding = $email_binding;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

SendEmail.php

<?php

namespace App\Listeners;

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

use App\Jobs\SendEmail as send;

class SendEmail
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  EmailLogin  $event
     * @return void
     */
    public function handle(EmailLogin $event)
    {
        $email = Send::dispatch($event);
    }
}

ルーティング(イベント()リスナー・トリガ・イベント)

Route::any('/login', function () {
    $email = "*******@qq.com";
    $email_binding = (object) ['email' => $email];

    event(new EmailLogin($email_binding)); //触发监听者事件
});

テスト
メールイベント
ジョブズ\のsendEmail

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Mail;
class SendEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $mail_binding;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($mail_binding)
    {
        $this->mail_binding = $mail_binding;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $mail_binding = $this->mail_binding;

        Mail::send('send',$mail_binding, function ($message) use ($mail_binding) {
            $message->to($mail_binding->email)->subject('登录验证');
        });

    }
}

オブザーバーパターンシナリオ:
  1.オブジェクトの状態を更新するために、他のオブジェクトが更新を同期する必要があり、他のオブジェクトを動的可変数
  2.オブジェクトが唯一の他のオブジェクトを知らなくても、他のオブジェクトへの通知を更新する必要がディテール

リリース6元記事 ウォンの賞賛0 ビュー327

おすすめ

転載: blog.csdn.net/weixin_42217143/article/details/104709717