Skip to content

Commit

Permalink
Allow for string events
Browse files Browse the repository at this point in the history
String events are especially useful for passing custom data to the listener. Consider the following:

```php
use Native\Laravel\Client\Client;
use Native\Laravel\Notification;

// Send a notification with a custom event
$client = new Client();
$notification = new Notification($client);

$articleId = 1;
$notification->title('EXTRA EXTRA READ ALL ABOUT IT')
    ->message('You should totally click this)
    ->event('notification.clicked.newArticle' . $articleId)
    ->show();

// Listen to the notification
Event::listen('notification.clicked.newArticle.*', function ($event) {
    $event = explode('.', $event)
    $articleId = array_last($event);
    // do stuff...
});
```
  • Loading branch information
LukeTowers authored and simonhamp committed Feb 7, 2024
1 parent e9a7532 commit 45b7ccf
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Http/Controllers/DispatchEventFromAppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ class DispatchEventFromAppController
public function __invoke(Request $request)
{
$event = $request->get('event');
if (class_exists($event)) {
$event = new $event(...$request->get('payload', []));
$payload = $request->get('payload', []);

if (class_exists($event)) {
$event = new $event(...$payload);
event($event);
} else {
event($event, $payload);
}

return response()->json([
Expand Down

0 comments on commit 45b7ccf

Please sign in to comment.