Skip to content

Commit 63a2acb

Browse files
author
Mostafa Kamal
committed
event
laravel event listener
1 parent 14faf3c commit 63a2acb

File tree

1 file changed

+124
-1
lines changed

1 file changed

+124
-1
lines changed
Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,128 @@
1+
---
2+
description: Event is a specific task and Listener listen a event then trigger one or many actions.
3+
keywords: laravel, php, event, listener
4+
title: Laravel Event Listener.
5+
toc_max: 4
6+
---
17

28

9+
* `Event` is a specific `task`.
10+
* `Listener` `listen` a `event` then `trigger` one or many `actions`.
11+
* `Actions<- Listener <- listen (event)`
12+
13+
```bash
14+
# generate from kernel
15+
php artisan event:generate
16+
# manually create
17+
php artisan make:listener ListenerName
18+
php artisan make:event EventName
19+
```
20+
21+
# event
22+
23+
* `EventName` class.
24+
25+
```php
26+
27+
namespace App\Events;
28+
29+
use Illuminate\Broadcasting\Channel;
30+
use Illuminate\Queue\SerializesModels;
31+
use Illuminate\Broadcasting\PrivateChannel;
32+
use Illuminate\Broadcasting\PresenceChannel;
33+
use Illuminate\Foundation\Events\Dispatchable;
34+
use Illuminate\Broadcasting\InteractsWithSockets;
35+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
36+
use App\User;
37+
38+
class EventName
39+
{
40+
use Dispatchable, InteractsWithSockets, SerializesModels;
41+
42+
public $user;
43+
44+
/**
45+
* Create a new event instance.
46+
*
47+
* @return void
48+
*/
49+
public function __construct(User $user)
50+
{
51+
$this->user = $user;
52+
}
53+
54+
/**
55+
* Get the channels the event should broadcast on.
56+
*
57+
* @return \Illuminate\Broadcasting\Channel|array
58+
*/
59+
public function broadcastOn()
60+
{
61+
return new PrivateChannel('channel-name');
62+
}
63+
}
64+
```
65+
66+
# listen
67+
68+
```php
69+
70+
namespace App\Listeners;
71+
72+
use App\Events\EventName;
73+
use Illuminate\Queue\InteractsWithQueue;
74+
use Illuminate\Contracts\Queue\ShouldQueue;
75+
76+
class ListenName
77+
{
78+
/**
79+
* Create the event listener.
80+
*
81+
* @return void
82+
*/
83+
public function __construct()
84+
{
85+
//
86+
}
87+
88+
/**
89+
* Handle the event.
90+
*
91+
* @param EventName $event
92+
* @return void
93+
*/
94+
public function handle(EventName $event)
95+
{
96+
return $event;
97+
}
98+
}
99+
```
100+
101+
# register
102+
103+
* `app\Providers\EventServiceProvider.php`
104+
105+
```php
106+
protected $listen = [
107+
'App\Events\EventName' => [
108+
'App\Listeners\ListenerName1',
109+
'App\Listeners\ListenerName2',
110+
'App\Listeners\ListenerNameALotOF',
111+
],
112+
];
113+
```
114+
115+
# fire / dispatching
116+
117+
```php
118+
use App\Events\OrderShipped;
119+
event(new EventName($order));
120+
```
121+
122+
123+
124+
# Resources
3125

4126
* [devlop video](https://www.youtube.com/watch?v=9oYtUEbT19I)
5-
* https://laravel.com/docs/5.7/events
127+
* [devlop queue event](https://www.youtube.com/watch?v=_SndYcQvIuQ)
128+
* [laravel.com](https://laravel.com/docs/5.7/events)

0 commit comments

Comments
 (0)