命令未定义异常

and*_*ber 12 command laravel laravel-4

我用Artisan创建了一个命令

$ php artisan command:make NeighborhoodCommand
Run Code Online (Sandbox Code Playgroud)

这创建了文件 app/commands/NeighborhoodCommand.php

代码片段.我修改了name值并填写了fire()函数

<?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
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试运行命令时

$ php artisan neighborhood
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

[InvalidArgumentException]
Command "neighborhood" is not defined.
Run Code Online (Sandbox Code Playgroud)

and*_*ber 27

Laravel 5.5+

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

如果您愿意,可以继续手动注册命令.但是L5.5为您提供了延迟加载它们的选项.如果要从旧版本升级,请将此方法添加到内核中:

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

    require base_path('routes/console.php');
}
Run Code Online (Sandbox Code Playgroud)

Laravel 5

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

编辑app/Console/Kernel.php文件并将命令添加到$commands数组:

protected $commands = [
    Commands\NeighborhoodCommand::class,
];
Run Code Online (Sandbox Code Playgroud)

Laravel 4

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

将此行添加到app/start/artisan.php:

Artisan::add(new NeighborhoodCommand);
Run Code Online (Sandbox Code Playgroud)

  • 加上谦虚和一个好的答案。发生在我身上1000次! (2认同)