2014-01-21 88 views
0

比方說,我有兩個聽衆在Laravel像這樣:Laravel監聽優先

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

我知道默認的監聽器的優先級設置爲0,但如果我想添加上述兩者之間的事件是什麼?

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

我該如何設置優先級?

回答

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'); 

PS :

您可以通過查看laravel資源來查看自己,例如Illuminate\Events\Dispatcher::sortListeners

P.P.S:

事件的優先級has been removed in Laravel 5.4