编辑:找出我出错的地方并将答案放在最后
我正在尝试创建一个 Laravel 命令,我可以看到它与 Laravel 3 中的“任务”相比发生了很大的变化。但是我似乎无法让它运行。这些是我已采取的步骤:
php artisan 命令:make 导入
Returns
命令创建成功
然后创建命令目录中的文件,我稍作修改以返回“Hello World”,如下所示:
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class Import extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'command:import';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
return 'Hello World';
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('example', InputArgument::REQUIRED, 'An example argument.'),
);
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}
}
但是,当我尝试像这样运行命令时:
php 工匠导入
我收到以下错误:
[InvalidArgumentException] 命令“导入”未定义。
我尝试过使用或不使用大写字母,并将其命名为“ImportCommand”,因为文档将其命令命名为“FooCommand”,但没有成功。
非常感激任何的帮助。
其实已经想通了这一点。文档进一步指出,您必须使用以下方法在“app/start/artisan.php”中注册您的命令:
Artisan::add(new import);
此外,您在命令类中给出的名称也很重要,因为您需要使用它来调用它。所以我实际上应该这样称呼它:
php artisan command:import
最后一件事。 fire() 返回的内容并不重要,要返回字符串,您必须回显它们。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)