【问题标题】:Laravel 5 Command and Handler issueLaravel 5 命令和处理程序问题
【发布时间】:2015-07-03 13:53:29
【问题描述】:

我正在使用 laravel 5 处理我的一个项目。在实施过程中,我遇到了一个与命令和处理程序相关的问题的结构。

我使用artisan命令生成命令

php artisan make:command TestCommand --handler

我在 app/commands 文件夹“TestCommand.php”中生成了命令

<?php

namespace App\Commands;

use App\Commands\Command;

class TestCommand extends Command
{
public $id;
public $name;

public function __construct($id, $name)
{
    $this->id = $id;
    $this->name = $name;
}
}

我的 TestCommandHandler.php 也是这样的

<?php

namespace App\Handlers\Commands;

use App\Commands\TestCommand;
use Illuminate\Queue\InteractsWithQueue;

class TestCommandHandler
{
/**
 * Create the command handler.
 *
 * @return void
 */
public function __construct()
{
    //
}

/**
 * Handle the command.
 *
 * @param  TestCommand  $command
 * @return void
 */
public function handle(TestCommand $command)
{
    dd($command);
}
}

每当从控制器发送此命令时,它都会显示以下问题

InvalidArgumentException in Dispatcher.php line 335:
No handler registered for command [App\Commands\TestCommand]

请有人帮我解决这个问题。谢谢

【问题讨论】:

    标签: laravel command


    【解决方案1】:

    默认情况下,Laravel 5.1.x 不包含 BusServiceProvider。所以我们应该在 provider 文件夹下创建 BusServiceProvider.php 并将其包含在 config/app.php 中。

    BusServiceProvider.php

    <?php namespace App\Providers;
    
    use Illuminate\Bus\Dispatcher;
    use Illuminate\Support\ServiceProvider;
    
    class BusServiceProvider extends ServiceProvider {
    
    /**
     * Bootstrap any application services.
     *
     * @param  \Illuminate\Bus\Dispatcher  $dispatcher
     * @return void
     */
    public function boot(Dispatcher $dispatcher)
    {
        $dispatcher->mapUsing(function($command)
        {
            return Dispatcher::simpleMapping(
                $command, 'App\Commands', 'App\Handlers\Commands'
            );
        });
    }
    
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
    
    }
    

    配置/app.php

    'providers' => [
        App\Providers\BusServiceProvider::class
    ]
    

    所以它可以帮助其他人。谢谢

    【讨论】:

      猜你喜欢
      • 2020-06-09
      • 1970-01-01
      • 2021-01-06
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      相关资源
      最近更新 更多