自定义laravel迁移命令"[Illuminate\Database\Migrations\MigrationRepositoryInterface]不可实例化"

Joh*_*lor 5 laravel laravel-5 artisan artisan-migrate laravel-5.2

我正在尝试创建一个自定义laravel(5.2)迁移命令,该命令基本上与migrate:status它相同,只是它只列出了挂起的迁移而不是所有迁移.

要做到这一点,我只是简单地将其复制migrate:status到我的app/console目录中的另一个类中,并调整代码以满足我的需要.但是每当我尝试运行它时,我都会收到错误:

[Illuminate\Contracts\Container\BindingResolutionException]构建[App\Console\Commands\PendingMigrations,Illuminate\Database\Migrations\Migrator]时,目标[Illuminate\Database\Migrations\MigrationRepositoryInterface]无法实例化.

类本身和fire()方法的内容似乎并不重要,因为它没有那么远,它在__construct()方法中失败了.

<?php namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Database\Migrations\Migrator;

class PendingMigrations extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'migrate:pending';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Shows a list of pending migrations';

    /**
     * The migrator instance.
     *
     * @var \Illuminate\Database\Migrations\Migrator
     */
    protected $migrator;

    /**
     * Create a new migration rollback command instance.
     *
     * @param  \Illuminate\Database\Migrations\Migrator $migrator
     * @return \Illuminate\Database\Console\Migrations\StatusCommand
     */
    public function __construct(Migrator $migrator)
    {
        parent::__construct();

        $this->migrator = $migrator;
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

它的原因很可能与IoC容器和加载内容的顺序有关,但我不太了解Laravel的内部工作原理,以了解更多内容.

肯定一定有可能吗?

我目前停留在5.2,所以我不确定这个问题是否存在于更新的版本中.

我到目前为止唯一尝试的是将迁移服务提供程序添加到列表顶部,config/app.php但它似乎没有影响,无论如何它只是随机猜测.

providers' => [
    Illuminate\Database\MigrationServiceProvider::class,`
]
Run Code Online (Sandbox Code Playgroud)

Joh*_*lor 9

我使用了这个:

$this->migrator = app('migrator');
Run Code Online (Sandbox Code Playgroud)

但这不一定是最好的方法

  • 这只花了1个小时.谢谢! (2认同)

pat*_*cus 5

Migrator实例未绑定到IoC容器类的名字,它被绑定到migrator别名。

来自Illuminate\Database\MigrationServiceProvider

/**
 * Register the migrator service.
 *
 * @return void
 */
protected function registerMigrator()
{
    // The migrator is responsible for actually running and rollback the migration
    // files in the application. We'll pass in our database connection resolver
    // so the migrator can resolve any of these connections when it needs to.
    $this->app->singleton('migrator', function ($app) {
        $repository = $app['migration.repository'];

        return new Migrator($repository, $app['db'], $app['files']);
    });
}
Run Code Online (Sandbox Code Playgroud)

由于类名未绑定在 IoC 容器中,当 Laravel 解析您的命令并尝试解析Migrator依赖项时,它会尝试从头开始构建一个新的并失败,因为Illuminate\Database\Migrations\MigrationRepositoryInterfaceIoC 容器中也未绑定该类名(因此您的错误'正在接收)。

由于 Laravel 自己无法解决这个问题,因此您需要为Migrator类名注册绑定,或者需要为您的命令注册绑定。Laravel 本身在Illuminate\Foundation\Providers\ArtisanServiceProvider. command.migrate绑定示例:

/**
 * Register the command.
 *
 * @return void
 */
protected function registerMigrateCommand()
{
    $this->app->singleton('command.migrate', function ($app) {
        return new MigrateCommand($app['migrator']);
    });
}
Run Code Online (Sandbox Code Playgroud)

因此,在您的AppServiceProvider或您设置的其他服务提供商中,您可以添加以下内容之一:

在 IoC 中注册命令:

$this->app->singleton(\App\Console\Commands\PendingMigrations::class, function ($app) {
    return new \App\Console\Commands\PendingMigrations($app['migrator']);
});
Run Code Online (Sandbox Code Playgroud)

或者,在 IoC 中注册 Migrator 类名:

$this->app->singleton(\Illuminate\Database\Migrations\Migrator::class, function ($app) {
    return $app['migrator'];
});
Run Code Online (Sandbox Code Playgroud)