2014-02-22 97 views
2

有没有办法轻松挂钩工匠命令?我期望完成的是每执行一次php artisan migrate命令都会执行一段代码。钩入laravel 4工匠命令

+0

您可以延长['MigrationServiceProvider'](https://github.com /illuminate/database/blob/33bfc4eb6fe9fca3ea9c5c0fb54acfbdb8f432bb/MigrationServiceProvider.php)或['MigrateCommand'](https://github.com/illuminate/database/blob/33bfc4eb6fe9fca3ea9c5c0fb54acfbdb8f432bb/Console/Migrations/MigrateCommand.php)调用。 – Sam

回答

0

您可以扩展架构构建门面和覆盖构建功能如下所示:

protected function build(Blueprint $blueprint) 
{ 
     your_function(); 
     Parent::build($blueprint); 
} 

然而,你的功能将不会被使用的迁移选项--pretend时调用。我不知道任何内置的方式来挂钩迁移,而不扩展Schema Builder或Migrator类。

2

只需在你的代码中添加一个监听器(甚至在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 
}); 
2

你可以尝试这样的事情(你可以把它放在filters.php文件):

Event::listen('artisan.start', function($app) { 
    if(isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == 'migrate') { 
     // do something because it's migrate command 
    } 
});