Laravelイベントevent
イベントは一般的な観察者モードの応用である.簡単に言えば...やる....これは...和乾...Laravelイベントでそれぞれ対応:
(event)... (listener)...
eventとlistenerファイルを配置する場所はそれぞれ:app/Events
app/Listeners
プロダクトマネージャにとって、イベントは主にあなたのビジネスロジックを規範化し、支線ロジックと主線ロジックを独立に分割するために使用されます.プログラマーにとって、イベントはControllerを非常に簡潔にし、デカップリングし、メンテナンスすることができます.イベントの定義(Event)
Artisanコマンドでテンプレートをすばやく生成できます:
php artisan event:generate
php
namespace App\Events;
use App\Podcast;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
class PodcastWasPurchased extends Event
{
use SerializesModels;
public $podcast;
/**
* Create a new event instance.
*
* @param Podcast $podcast
* @return void
*/
public function __construct(Podcast $podcast)
{
$this->podcast = $podcast;
}
}
, , DTL(Data Transpotation Layer), , 。
(Listener and Handler)
artisan Event , Listner :
php
namespace App\Listeners;
use App\Events\PodcastWasPurchased;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class EmailPurchaseConfirmation
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param PodcastWasPurchased $event
* @return void
*/
public function handle(PodcastWasPurchased $event)
{
// Access the podcast using $event->podcast...
}
}
handler , type-hint , 。
Event Listener
Service Provider:
providers/EventServiceProvider.php
Listener:
protected $listen = [
'App\Events\PodcastWasPurchased' => [
'App\Listeners\EmailPurchaseConfirmation',
],
];
, controller :
php
namespace App\Http\Controllers;
use Event;
use App\Podcast;
use App\Events\PodcastWasPurchased;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* Show the profile for the given user.
*
* @param int $userId
* @param int $podcastId
* @return Response
*/
public function purchasePodcast($userId, $podcastId)
{
$podcast = Podcast::findOrFail($podcastId);
// Purchase podcast logic...
Event::fire(new PodcastWasPurchased($podcast));
}
}
Event::fire(new PodcastWasPurchased($podcast));
, , listener(handler)。Event::fire()
:
event(new PodcastWasPurchased($podcast));
, , , 。
Listener :
php
namespace App\Listeners;
use App\Events\PodcastWasPurchased;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class EmailPurchaseConfirmation implements ShouldQueue
{
//
}
implements ShouldQueue 。
:
php
namespace App\Listeners;
use App\Events\PodcastWasPurchased;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class EmailPurchaseConfirmation implements ShouldQueue
{
use InteractsWithQueue;
public function handle(PodcastWasPurchased $event)
{
if (true) {
$this->release(10);
}
}
}
10 。
(Event Subscribers)
Event Subscribers Listener, listener hander(), (handler) , listner , listner 。
php
namespace App\Listeners;
class UserEventListener
{
/**
* Handle user login events.
*/
public function onUserLogin($event) {}
/**
* Handle user logout events.
*/
public function onUserLogout($event) {}
/**
* Register the listeners for the subscriber.
*
* @param Illuminate\Events\Dispatcher $events
* @return array
*/
public function subscribe($events)
{
$events->listen(
'App\Events\UserLoggedIn',
'App\Listeners\UserEventListener@onUserLogin'
);
$events->listen(
'App\Events\UserLoggedOut',
'App\Listeners\UserEventListener@onUserLogout'
);
}
}
subscribe(), 。
Event Subscriber Service Provider
php
namespace App\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
//
];
/**
* The subscriber classes to register.
*
* @var array
*/
protected $subscribe = [
'App\Listeners\UserEventListener',
];
}
Event
Event , , 。
Event, $event->sendWelcomeMessage($user) , , ?, 『 』,Fire 。 , , , , , ( ... ), 。
, , , , , , 。 $event->sendWelcomeMessage($user) hardcoding , sendWelcomeMessage 。
leader, , , , ( ) , 。
, Event , , 。
Event , 。