Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a listener for console commands

I want to run a method when every single console command is run, How do I create a listener for all console commands in lumen?

I tried by creating a listener for ConsoleCommandEvent, this event is fired in \Symfony\Component\Console\Application::doRunCommand, but event dispatcher has not been attached (read the comment above the method: If an event dispatcher has been attached to the application, events are also dispatched during the life-cycle of the command.)

Update Using this event: Illuminate\Console\Events\ArtisanStarting might help to do the job, but it is a different event. By doing so any time that you run php artisan your code will be executed whether you run an actual command or not.

like image 469
Mohammad Mehdi Habibi Avatar asked Sep 26 '16 11:09

Mohammad Mehdi Habibi


1 Answers

In Laravel you can listen to the CommandStarting event which is also available in illuminate/console.

like image 62
Olivenbaum Avatar answered Oct 06 '22 00:10

Olivenbaum