【问题标题】:Laravel package custom migration commandLaravel 包自定义迁移命令
【发布时间】:2020-09-20 18:57:35
【问题描述】:

我想制作一个包,它使用自定义存根来创建我的包所需的迁移。更准确地说,运行该命令应该为具有特定特征的模型制作数据透视表。

如果我发出“正常”命令,我可以在我的服务提供商中注册它:

public function boot()
{
    if ($this->app->runningInConsole()) {

        $this->commands([
            MakeContainerMigration::class,
        ]);
    }
}

但是在这种情况下,我想重用 Laravel 的编码,省去重新发明轮子的麻烦。所以我的命令看起来像这样:

class MakeContainerMigration extends MigrateMakeCommand
{
    protected $name = 'custom:make-container';

    protected $description = '...';
}

由于MigrateMakeCommand 没有定义存根,而是依赖于MigrationCreator,我需要找到一种方法来为其提供自定义存根路径,而不会破坏“常规”迁移存根。

我尝试做这样的事情但失败了:

public function register()
{
    $this->registerCreator();
    $this->registerMigrateMakeCommand();

    if ($this->app->runningInConsole()) {

        $this->commands([
            //MakeContainerMigration::class,
            'custom.command.migrate.make'
        ]);
    }
}

protected function registerCreator()
{
    $this->app->singleton('custom.migration.creator', function ($app) {
        return new MigrationCreator($app['files'], __DIR__ . '/stubs');
    });
}

protected function registerMigrateMakeCommand()
{
    $this->app->singleton('custom.command.migrate.make', function ($app) {
        $creator = $app['custom.migration.creator'];
        $composer = $app['composer'];

        return new MakeContainerMigration($creator, $composer);
    });
}

我知道注册命令不应该像这样运行,因为我只是将单例注册到 Laravel app 实例,但我不知道如何通过类注册它,同时确保 @ 的正确版本987654327@ 将被注入。我有点卡在这里,有什么办法吗?

【问题讨论】:

    标签: php laravel service-provider


    【解决方案1】:

    原来一切正常,我只需要更换

    protected $name = 'custom:make-container';
    

    protected $signature = 'custom:make-container';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-21
      • 2018-02-06
      • 2020-07-17
      • 2018-06-11
      • 2018-04-15
      • 2020-10-24
      • 2021-12-31
      • 2015-07-05
      相关资源
      最近更新 更多