Readouble

Laravel 5.dev イベント

基本的な使用法Basic Usage

Laravelのイベント機能は、シンプルなオブザーバの実装を提供します。アプリケーションの中でイベントを購読したり、リッスンするために使用します。イベントクラスは通常、app/Eventsディレクトリーへ保存され、ハンドラーはapp/Handlers/Eventsに設置します。The Laravel event facilities provides a simple observer implementation, allowing you to subscribe and listen for events in your application. Event classes are typically stored in the app/Events directory, while their handlers are stored in app/Handlers/Events.

新しいイベンクラスは、Artisan CLIツールを使用して生成します。You can generate a new event class using the Artisan CLI tool:

php artisan make:event PodcastWasPurchased

イベントを購入するSubscribing To An Event

Laravelアプリケーションに含まれているEventServiceProviderは、全イベントハンドラーを登録するための便利な手段を提供しています。listenプロパティは全イベント(キー)とハンドラー(値)で構成されている配列です。もちろん、アプリケーションで必要とされているイベントをこの配列に好きなだけ追加できます。例えば、PodcastWasPurchasedイベントを追加してみましょう。The EventServiceProvider included with your Laravel application provides a convenient place to register all event handlers. The listen property contains an array of all events (keys) and their handlers (values). Of course, you may add as many events to this array as your application requires. For example, let's add our PodcastWasPurchased event:

/**
 * アプリケーションのイベントハンドラーをマップする
 *
 * @var array
 */
protected $listen = [
	'App\Events\PodcastWasPurchased' => [
		'App\Handlers\Events\EmailPurchaseConfirmation@handle',
	],
];

イベントハンドラーを生成するには、handler:event Artisan CLIコマンドを使用します。To generate a handler for an event, use the handler:event Artisan CLI command:

php artisan handler:event EmailPurchaseConfirmation --event=PodcastWasPurchased

ハンドラーやイベントが必要になるごとに毎回、make:eventhandler:eventコマンドを実行するのは、もちろん手間がかかります。代わりに、ハンドラーとイベントをEventServiceProviderに追加し、event:generateコマンドを使いましょう。このコマンドはEventServiceProviderにリストしてあるイベントやハンドラーを生成してくれます。Of course, manually running the make:event and handler:event commands each time you need a handler or event is cumbersome. Instead, simply add handlers and events to your EventServiceProvider and use the event:generate command. This command will generate any events or handlers that are listed in your EventServiceProvider:

php artisan event:generate

イベントの発行Firing An Event

これで、Eventファサードを使用し、イベントを発行する用意ができました。Now we are ready to fire our event using the Event facade:

$response = Event::fire(new PodcastWasPurchased($podcast));

アプリケーションの中で、次に起きることをコントロールするために、fireメソッドはレスポンスの配列を返します。The fire method returns an array of responses that you can use to control what happens next in your application.

イベントを起こすために、eventヘルパを使用することもできます。You may also use the event helper to fire an event:

event(new PodcastWasPurchased($podcast));

クロージャーのリスナーClosure Listeners

イベントを購読するために、独立したクラスを作成する必要は全くありません。例えば、EventServiceProviderbootメソッドの中で、以下のように指定できます。You can even listen to events without creating a separate handler class at all. For example, in the boot method of your EventServiceProvider, you could do the following:

Event::listen('App\Events\PodcastWasPurchased', function($event)
{
	// イベントの処理…
});

イベントの伝播を止めるStopping The Propagation Of An Event

場合により、イベントが他のリスナーへ伝播されるのを止めたいこともあります。ハンドラーから、falseを返してください。Sometimes, you may wish to stop the propagation of an event to other listeners. You may do so using by returning false from your handler:

Event::listen('App\Events\PodcastWasPurchased', function($event)
{
	// イベントの処理…

	return false;
});

キュー済みイベントハンドラーQueued Event Handlers

イベントハンドラーを[キュー]queueに投入する必要があるのですか?ハンドラを生成する時に、--queuedフラッグを付けるだけです。Need to queue[/docs/master/queues] an event handler? It couldn't be any easier. When generating the handler, simply use the --queued flag:

php artisan handler:event SendPurchaseConfirmation --event=PodcastWasPurchased --queued

これにより、ハンドラクラスへIlluminate\Contracts\Queue\ShouldBeQueuedインターフェイスが実装されます。これだけです!これで、このハンドラーがイベントにより呼び出されると、イベントデスパッチャーにより自動的にキューへ投入されます。This will generate a handler class that implements the Illuminate\Contracts\Queue\ShouldBeQueued interface. That's it! Now when this handler is called for an event, it will be queued automatically by the event dispatcher.

キューされたハンドラーの実行時に、例外が投げられなければ、そのキューは処理後自動的に削除されます。もし、deletereleaseメソッドへ手動でアクセスする必要があれば、どうぞそうしてください。デフォルトでキューハンドラーに含まれている、Illuminate\Queue\InteractsWithQueueトレイトが、それらのメソッドへのアクセスを提供しています。If no exceptions are thrown when the handler is executed by the queue, the queued job will be deleted automatically after it has processed. If you need to access the queued job's delete and release methods manually, you may do so. The Illuminate\Queue\InteractsWithQueue trait, which is included by default on queued handlers, gives you access to these methods:

public function handle(PodcastWasPurchased $event)
{
	if (true)
	{
		$this->release(30);
	}
}

既に存在しているハンドラーをキューへ投入するハンドラーへ変更したい場合は、ShouldBeQueuedインターフェイスをそのクラスに追加するだけです。If you have an existing handler that you would like to convert to a queued handler, simply add the ShouldBeQueued interface to the class manually.

イベント購読クラスEvent Subscribers

イベント購読クラスを定義するDefining An Event Subscriber

イベント購読クラスは一つのクラスで多くのイベントをリッスンするためのものです。購買クラスは、イベントデスパッチャーインスタンスが渡されるsubscribeメソッドを実装しなくてはなりません。Event subscribers are classes that may subscribe to multiple events from within the class itself. Subscribers should define a subscribe method, which will be passed an event dispatcher instance:

class UserEventHandler {

	/**
	 * ユーザーログインイベントの処理
	 */
	public function onUserLogin($event)
	{
		//
	}

	/**
	 * ユーザーログアウトイベントの処理
	 */
	public function onUserLogout($event)
	{
		//
	}

	/**
	 * イベント購入リスナーの登録
	 *
	 * @param  Illuminate\Events\Dispatcher  $events
	 * @return array
	 */
	public function subscribe($events)
	{
		$events->listen('App\Events\UserLoggedIn', 'UserEventHandler@onUserLogin');

		$events->listen('App\Events\UserLoggedOut', 'UserEventHandler@onUserLogout');
	}

}

イベント購入クラスを登録するRegistering An Event Subscriber

イベント購読クラスを定義したら、Eventクラスで登録しましょう。Once the subscriber has been defined, it may be registered with the Event class.

$subscriber = new UserEventHandler;

Event::subscribe($subscriber);

また、LaravelのIoCコンテナを使用し、購読クラスを依存解決したい場合もあるでしょう。その場合でも、ただsubscribeメソッドへ、購読クラスの登録名を渡すだけです。You may also use the Laravel IoC container[/docs/ioc] to resolve your subscriber. To do so, simply pass the name of your subscriber to the subscribe method:

Event::subscribe('UserEventHandler');

章選択

Artisan CLI

設定

明暗テーマ
light_mode
dark_mode
brightness_auto システム設定に合わせる
テーマ選択
photo_size_select_actual デフォルト
photo_size_select_actual モノクローム(白黒)
photo_size_select_actual Solarized風
photo_size_select_actual GitHub風(青ベース)
photo_size_select_actual Viva(黄緑ベース)
photo_size_select_actual Happy(紫ベース)
photo_size_select_actual Mint(緑ベース)
コードハイライトテーマ選択

明暗テーマごとに、コードハイライトのテーマを指定できます。

テーマ配色確認
スクリーン表示幅
640px
80%
90%
100%

768px以上の幅があるときのドキュメント部分表示幅です。

インデント
無し
1rem
2rem
3rem
原文確認
原文を全行表示
原文を一行ずつ表示
使用しない

※ 段落末のEボタンへカーソルオンで原文をPopupします。

Diff表示形式
色分けのみで区別
行頭の±で区別
削除線と追記で区別

※ [tl!…]形式の挿入削除行の表示形式です。

テストコード表示
両コード表示
Pestのみ表示
PHPUnitのみ表示
和文変換

対象文字列と置換文字列を半角スペースで区切ってください。(最大5組各10文字まで)

本文フォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

コードフォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

保存内容リセット

localStrageに保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作