2014-02-22 130 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 
    } 
});