Laravel听众优先事项

时间:2014-01-21 12:58:00

标签: php events laravel

假设我在Laravel中有两个听众:

Event::listen('eventOne', 'myListenerOne');
Event::listen('eventThree', 'myListenerThree');

我知道默认情况下,侦听器优先级设置为0.但是如果我想在上面两个之间添加一个事件呢?

Event::listen('eventTwo', 'myListenerTwo');

我会将优先级设置为什么?

1 个答案:

答案 0 :(得分:0)

Laravel< = 5.3

只需添加更高优先级的侦听器,例如1

Event::listen('eventTwo', 'myListenerTwo', 1);

这是一个更好地理解事件优先级的小例子

Event::listen(
     'test', function () {
             echo 'On test, first added but last executed', PHP_EOL;
         }, -1);


Event::listen(
     'test', function () {
             echo 'On test, added in the middle and executed in the middle', PHP_EOL;
         });


Event::listen(
     'test', function () {
             echo 'On test, last added but first executed', PHP_EOL;
         }, 1);

Event::fire('test');

P.S:

您可以随时查看laravel来源,例如Illuminate\Events\Dispatcher::sortListeners

P.P.S:

活动优先级has been removed in Laravel 5.4