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
Command is not defined exception
提问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 name
value 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.php
file and add your command to the $commands
array:
编辑您的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);