有没有一种简单的方法可以钩入 Artisan 命令?我想要实现的是,每次执行 php artisan migrate
命令时都会执行一段代码。
filters.php
文件中):Event::listen('artisan.start', function($app) {
if( isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == 'migrate' ) {
// do something because it's migrate command
}
});
在代码的某个位置(即使是在 app/start/artisan.php 的顶部),添加一个监听器来监听 'artisan.start' 事件。
Event::listen('artisan.start', function($app)
{
// $app is instance of Illuminate\Console\Application
// since the $app hasn't figured the command yet you'll
// have to do it yourself to check if it's the migrate command
});
protected function build(Blueprint $blueprint)
{
your_function();
Parent::build($blueprint);
}
然而,当使用迁移的 --pretend 选项时,您的函数不会被调用。我不知道有没有内置的方法可以在不扩展模式生成器或迁移器类的情况下钩入迁移。
MigrationServiceProvider
或者它调用的MigrateCommand
。 - Sam