Command is not defined exception

走远了吗. 提交于 2020-02-23 08:49:19

问题


I created a command with Artisan

$ php artisan command:make NeighborhoodCommand

This created the file app/commands/NeighborhoodCommand.php

Snippet of the code. I modified the name value and filled in the fire() function

<?php

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class NeighborhoodCommand extends Command {

    protected $name = 'neighborhood';

    public function fire()
    {
        // my code
    }
}

But then when I try to run the command with

$ php artisan neighborhood

I get this error:

[InvalidArgumentException]
Command "neighborhood" is not defined.

回答1:


Laravel 5.5+

https://laravel.com/docs/5.5/artisan#registering-commands

If you'd like, you can continue manually registering your commands. But L5.5 gives you the option to lazy load them. If you are upgrading from an older version add this method to your Kernel:

/**
 * Register the commands for the application.
 *
 * @return void
 */
protected function commands()
{
    $this->load(__DIR__ . '/Commands');

    require base_path('routes/console.php');
}

Laravel 5

http://laravel.com/docs/5.4/artisan#registering-commands

Edit your app/Console/Kernel.php file and add your command to the $commands array:

protected $commands = [
    Commands\NeighborhoodCommand::class,
];

Laravel 4

http://laravel.com/docs/4.2/commands#registering-commands

Add this line to app/start/artisan.php:

Artisan::add(new NeighborhoodCommand);



回答2:


By changing signature in your command class to your commandName. change

protected $signature = 'command:name';

TO

protected $signature = 'NeighborhoodCommand';

Now try to run

$ php artisan command:make NeighborhoodCommand

It works for me.

Reference



来源:https://stackoverflow.com/questions/23462266/command-is-not-defined-exception

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