Skip to content

[9.x] Fix registering event listeners with array callback#43890

Merged
taylorotwell merged 1 commit into
laravel:9.xfrom
Digity101:9.x
Aug 29, 2022
Merged

[9.x] Fix registering event listeners with array callback#43890
taylorotwell merged 1 commit into
laravel:9.xfrom
Digity101:9.x

Conversation

@Digity101

@Digity101 Digity101 commented Aug 27, 2022

Copy link
Copy Markdown
Contributor
  • Laravel Version: 9.26
  • PHP Version: 8.1.1

Description:

In Laravel 9.x according to the docs, you could register listeners with in EventServiceProvider with the array syntax. I.E

class EventServiceProvider extends ServiceProvider { 
  /**
   * The event listener mappings for the application.
   *
   * @var array
   */
  protected $listen = [
      OrderShipped::class => [
          [SendShipmentNotification::class, 'handle'],
      ],
  ];
}

However, if you register multiple listeners with the array syntax, then you get an array to string conversion error

Steps To Reproduce:

class EventServiceProvider extends ServiceProvider { 
 /**
  * The event listener mappings for the application.
  *
  * @var array
  */
 protected $listen = [
     OrderShipped::class => [
         [SendShipmentNotification::class, 'handle'],
         [SendShipmentNotification::class, 'handle2'],
     ],
 ];
}

Fix:

The EventServiceProvider uses array_unique to get unique listeners, however the default behavior is to compare items as strings. Giving the flag SORT_REGULAR fixes the issue.

@taylorotwell
taylorotwell merged commit b32808d into laravel:9.x Aug 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants