我希望在运行 artisan 命令时运行一个自定义的中间件。在实际命令执行之前,我需要设置一些要在应用程序配置中使用的环境变量,但我找不到有关 Artisan 请求生命周期的任何文档。
这是可能的吗?
我找到了一种方法,不确定是否正确,但有效。只需打开 Kernel 类并重写 bootstrap 方法即可。Laravel 5.3,尚未在其他版本上进行测试,但应该可以类似地工作。
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
}
}
Run Code Online (Sandbox Code Playgroud)