Skip to content

Commit

Permalink
working on event docs
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jun 1, 2015
1 parent 262a546 commit 2a2a911
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions events.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
- [Registering Events / Listeners](#registering-events-and-listeners)
- [Defining Events](#defining-events)
- [Defining Listeners](#defining-listeners)
- [Queued Event Listeners](#queued-event-listeners)
- [Firing Events](#firing-events)
- [Queued Event Listeners](#queued-event-listeners)
- [Event Subscribers](#event-subscribers)

<a name="introduction"></a>
Expand Down Expand Up @@ -114,45 +114,8 @@ Your event listeners may also type-hint any dependencies they need on their cons

Sometimes, you may wish to stop the propagation of an event to other listeners. You may do so using by returning `false` from your listener's `handle` method.

<a name="firing-events"></a>
## Firing Events

To fire an event, you may use the `Event` [facade](/docs/{{version}}/facades), passing an instance of the event to the `fire` method. The `fire` method will dispatch the event to all of its registered listeners:

event(new PodcastWasPurchased($podcast));

<?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));
}
}

Alternatively, you may use the global `event` helper function to fire events:

event(new PodcastWasPurchased($podcast));

<a name="queued-event-listeners"></a>
## Queued Event Listeners
### Queued Event Listeners

Need to [queue](/docs/{{version}}/queues) an event listener? It couldn't be any easier. Simply add the `ShouldQueue` interface to the listener class. Listeners generated by the `event:generate` Artisan command already have this interface imported into the current namespace, so you can use it immediately:

Expand Down Expand Up @@ -191,6 +154,43 @@ If you need to access the underlying queue job's `delete` and `release` methods
}
}

<a name="firing-events"></a>
## Firing Events

To fire an event, you may use the `Event` [facade](/docs/{{version}}/facades), passing an instance of the event to the `fire` method. The `fire` method will dispatch the event to all of its registered listeners:

event(new PodcastWasPurchased($podcast));

<?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));
}
}

Alternatively, you may use the global `event` helper function to fire events:

event(new PodcastWasPurchased($podcast));

<a name="event-subscribers"></a>
## Event Subscribers

Expand Down

0 comments on commit 2a2a911

Please sign in to comment.