命令未定义异常

时间:2014-05-04 22:24:53

标签: laravel laravel-4 command

我用Artisan

创建了一个命令
$ php artisan command:make NeighborhoodCommand

这创建了文件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
    }
}

但是当我尝试用

运行命令时
$ php artisan neighborhood

我收到此错误:

[InvalidArgumentException]
Command "neighborhood" is not defined.

3 个答案:

答案 0 :(得分:26)

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

Laravel 5

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

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

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

Laravel 4

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

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

Artisan::add(new NeighborhoodCommand);

答案 1 :(得分:2)

通过将命令类中的 签名 更改为 commandName 。 更改

protected $signature = 'command:name';

TO

protected $signature = 'NeighborhoodCommand';

现在尝试运行

$ php artisan NeighborhoodCommand

对我有用。

Reference

答案 2 :(得分:1)

如果以上方法都不适合您,您可能想尝试 composer dumpautoload,它为我解决了问题。

相关问题