Events Flashcards

1
Q

In what directory are event classes stored?

A

Event classes are typically stored in the app/Events directory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In what directory is listener classes stored?

A

Listeners are stored in app/Listeners

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Where is a convenient place to register all of your application’s event listeners?

A

The EventServiceProvider

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the purpose of the EventServiceProvider listen property?

A

The listen property contains an array of all events (keys) and their listeners (values). Of course, you may add as many events to this array as your application requires. For example, let’s add a OrderShipped event:

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'App\Events\OrderShipped' => [
        'App\Listeners\SendShipmentNotification',
    ],
];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the purpose of the php artisan event:generate command?

A

Of course, manually creating the files for each event and listener is cumbersome. Instead, simply add listeners and events to your EventServiceProvider and use the event:generate command. This command will generate any events or listeners that are listed in your EventServiceProvider. Of course, events and listeners that already exist will be left untouched:

php artisan event:generate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of a wildcard event listener?

A

You may even register listeners using the * as a wildcard parameter, allowing you to catch multiple events on the same listener. Wildcard listeners receive the event name as their first argument, and the entire event data array as their second argument:

Event::listen('event.*', function ($eventName, array $data) {
    //
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of the SerializeModels trait used by an event class?

A

The SerializesModels trait used by the event will gracefully serialize any Eloquent models if the event object is serialized using PHP’s serialize function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a event class?

A

An event class is simply a data container which holds the information related to the event.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a listener class?

A

Event listeners receive the event instance in their handle method. Within the handle method, you may perform any actions necessary to respond to the event:

public function handle(OrderShipped $event)
{
    // Access the order using $event->order...
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How would you stop the propagation of an event?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the benefit of utilizing queued event listeners?

A

Queueing listeners can be beneficial if your listener is going to perform a slow task such as sending an e-mail or making an HTTP request.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would you specify that a listener should be queued?

A

To specify that a listener should be queued, 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:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happens to a queued listener if no exceptions are thrown after being executed by the queue?

A

The queued job will automatically be deleted after it has finished processing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How would you customize the queue connection and queue name used by an event listener?

A

If you would like to customize the queue connection and queue name used by an event listener, you may define $connection and $queue properties on your listener class:

/**
 * The name of the connection the job should be sent to.
 *
 * @var string|null
 */
public $connection = 'sqs';
/**
 * The name of the queue the job should be sent to.
 *
 * @var string|null
 */
public $queue = 'listeners';
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the trait required by a listener to access a queue’s job delete and release methods?

A

The Illuminate\Queue\InteractsWithQueue trait:

release(30);
}
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the name of the method that will be called on a listener class if the queued listener exceeds the maximum number of attempts as defined by your queue worker?

A

The failed method will be called on your listener.

17
Q

If a failed method is called on your event listener, what does it receive?

A

The failed method receives the event instance and the exception that caused the failure:

public function failed(OrderShipped $event, $exception)
{
    //
}
18
Q

How would you dispatch an event?

A

To dispatch an event, you may pass an instance of the event to the event helper. The helper will dispatch the event to all of its registered listeners. Since the event helper is globally available, you may call it from anywhere in your application:

event(new OrderShipped($order));

19
Q

What is the definition of a event subscriber?

A

Event subscribers are classes that may subscribe to multiple events from within the class itself, allowing you to define several event handlers within a single class.