Laravel 5 Command and Handler issue

╄→гoц情女王★ 提交于 2019-12-08 05:26:39

问题


I am working one of my project with laravel 5. During the implementation i got struct with one issue which is related to command and handler.

I used artisan command to generate command

php artisan make:command TestCommand --handler

I generated command at app/commands folder "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;
}
}

Also my TestCommandHandler.php looks like this

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

Whenever dispatch this command from controller it shows following issue

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

Please, Anybody help me to solve this problem. Thank you


回答1:


By default Laravel 5.1.x does not included BusServiceProvider. So we should create BusServiceProvider.php under provider folder and include that in to 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()
{
    //
}

}

config/app.php

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

So it may help others. Thank you



来源:https://stackoverflow.com/questions/31208519/laravel-5-command-and-handler-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!