Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Aug 29, 2022

Conversation

Digity101
Copy link
Contributor

@Digity101 Digity101 commented Aug 27, 2022

  • 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