Command is not defined exception
Asked Answered
S

5

23

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.
Satiate answered 4/5, 2014 at 22:24 Comment(0)
S
35

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);
Satiate answered 4/5, 2014 at 22:27 Comment(2)
Plus for being humble and a good answer. Happened to me 1000 times!Luedtke
Laravel 5.5+ PHP Fatal error: Cannot redeclare App\Console\Kernel::commands()Erythroblast
C
3

if none of the above work for you you might want to try to composer dumpautoload which fixed for me.

Chronogram answered 14/1, 2021 at 19:26 Comment(0)
S
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 NeighborhoodCommand

It works for me.

Reference

Stroboscope answered 11/2, 2020 at 7:43 Comment(0)
T
2

Just in case

I tried all the above and I would still get the "Command is not defined" message (even after a composer dumpautoload). This was before I embarrassingly realised my command file name was not binding the Command Class name.

I had

Filename : CommandThatDoesThis.php

Class: CommandThatDoesThat

Ran a composer dumpautoload just after and all was fine.

note: this would have never happened if I created the command properly, using the php artisan make:command. I ended up in this situation because I duplicated an existing command.

Therewithal answered 27/8, 2021 at 12:6 Comment(0)
E
0

you should also note, that if you fail to put in the proper value to $signature, you will get the same error

Engvall answered 25/4, 2023 at 13:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.