连接到Laravel 4的Artisan命令

3

有没有一种简单的方法可以钩入 Artisan 命令?我想要实现的是,每次执行 php artisan migrate 命令时都会执行一段代码。


你可以扩展 MigrationServiceProvider 或者它调用的 MigrateCommand - Sam
3个回答

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
    }
});

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
});

0
您可以扩展Schema Builder门面并像这样覆盖build函数:
protected function build(Blueprint $blueprint)
{
      your_function();
      Parent::build($blueprint);
}

然而,当使用迁移的 --pretend 选项时,您的函数不会被调用。我不知道有没有内置的方法可以在不扩展模式生成器或迁移器类的情况下钩入迁移。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接