Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Middleware when running Artisan console request

I want a custom piece of middleware to run whenever I run an artisan command. I need to set a few environment variables to be used in the app configuration before the actual command executes but Im not able to find any documentation on the Artisan request lifecycle.

Is this possible to do?

like image 459
stephenthedev Avatar asked May 12 '16 13:05

stephenthedev


1 Answers

I found a way, not sure if the correct one, but works. Just open up the Kernel class and override the bootstrap method. Laravel 5.3, have not tested on other versions, but should work similiary.

class Kernel extends ConsoleKernel
{
    protected $commands = [
        // Your commands here
    ];

    public function bootstrap()
    {
        // Don't forget to call parent bootstrap
        parent::bootstrap();

        // Do your own bootstrapping stuff here
    }

    protected function schedule(Schedule $schedule)
    {
        // Add cron schedules here, if needed
    }
}
like image 70
Mārtiņš Briedis Avatar answered Nov 15 '22 09:11

Mārtiņš Briedis