laravel 命令未定义异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23462266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:28:31  来源:igfitidea点击:

Command is not defined exception

laravellaravel-4command

提问by andrewtweber

I created a command with Artisan

我用 Artisan 创建了一个命令

$ php artisan command:make NeighborhoodCommand

This created the file app/commands/NeighborhoodCommand.php

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

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

代码片段。我修改了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
    }
}

But then when I try to run the command with

但是当我尝试运行命令时

$ php artisan neighborhood

I get this error:

我收到此错误:

[InvalidArgumentException]
Command "neighborhood" is not defined.

回答by andrewtweber

Laravel 5.5+

Laravel 5.5+

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

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:

如果您愿意,可以继续手动注册您的命令。但是 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

Laravel 5

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

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

Edit your app/Console/Kernel.phpfile and add your command to the $commandsarray:

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

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


Laravel 4

Laravel 4

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

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

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

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

Artisan::add(new NeighborhoodCommand);

回答by Satish

By changing signaturein your command class to your commandName. change

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

protected $signature = 'command:name';

TO

protected $signature = 'NeighborhoodCommand';

Now try to run

现在尝试运行

$ php artisan NeighborhoodCommand

It works for me.

这个对我有用。

Reference

参考