簡體   English   中英

鈎入laravel 4工匠指揮部

[英]Hook into laravel 4 artisan command

有沒有一種方法可以輕松地加入工匠命令? 我要完成的工作是,每次執行php artisan migrate命令時都會執行一段代碼。

只需在代碼中的某個位置(甚至在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
});

您可以嘗試這樣的操作(您可以將其放入filters.php文件中):

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

您可以擴展Schema Builder外觀並覆蓋構建功能,如下所示:

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

但是,使用--pretend選項進行遷移時,不會調用您的函數。 我不知道在不擴展Schema Builder或Migrator類的情況下,是否有任何內置方法可以掛接到遷移中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM