创建和使用 Laravel 4 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17172012/
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
Creating and using Laravel 4 commands
提问by John Mellor
EDIT: Figured out where I was going wrong and placed an answer at the end
编辑:找出我出错的地方并在最后给出答案
I'm trying to create a Laravel Command, I can see it's changed considerably from "tasks" in Laravel 3. However I can't seem to get it to run. These are the steps I have taken:
我正在尝试创建一个 Laravel 命令,我可以看到它与 Laravel 3 中的“任务”相比发生了很大变化。但是我似乎无法让它运行。这些是我采取的步骤:
php artisan command:make Import
php artisan 命令:make 导入
Returns
退货
Command created successfully
命令创建成功
The file in the commands directory is then created and I have slightly modified to return "Hello World" like so:
然后创建命令目录中的文件,我稍作修改以返回“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),
);
}
}
However when I try and run the command like so:
但是,当我尝试像这样运行命令时:
php artisan Import
php工匠导入
I get the following error:
我收到以下错误:
[InvalidArgumentException] Command "Import" is not defined.
[InvalidArgumentException] 命令“导入”未定义。
I have tried it with and without capitals as well as naming it "ImportCommand" since the documentation names its command "FooCommand" but with no luck.
我尝试过使用和不使用大写字母以及将其命名为“ImportCommand”,因为文档将其命令命名为“FooCommand”,但没有运气。
Any help would be most appreciated.
非常感激任何的帮助。
回答by John Mellor
Actually figured this out. Further down the documentation it states that you must register your command in "app/start/artisan.php" using the following method:
其实想通了这一点。在文档的进一步下方,它指出您必须使用以下方法在“app/start/artisan.php”中注册您的命令:
Artisan::add(new import);
Also the name you give in your command class is significant as that's what you need to use to call it. So I should have actually been calling it like so:
此外,您在命令类中给出的名称也很重要,因为您需要使用它来调用它。所以我实际上应该这样称呼它:
php artisan command:import
One final thing. What the fire() returns is unimportant, to return strings you must echo them.
最后一件事。fire() 返回的内容并不重要,要返回字符串,您必须回显它们。
回答by user3228017
try this.
尝试这个。
protected function getArguments()
{
return [];
}
protected function getOptions()
{
return [];
}
also add this in /app/start/artisan.php
也加入这个 /app/start/artisan.php
Artisan::add(new ParseCommand);
then Run Command on Root directory
然后在根目录上运行命令
./artisan command:import;
回答by totymedli
In newer Laravel versions there isn't an import
command. You just have to do the following two thing:
在较新的 Laravel 版本中,没有import
命令。你只需要做以下两件事:
Register your command in
app/start/artisan.php
:Artisan::add(new Import);
Run the command in Artisan:
php artisan command:name Import
在
app/start/artisan.php
以下位置注册您的命令:Artisan::add(new Import);
在 Artisan 中运行命令:
php artisan command:name Import
回答by yassine
there's a mis-understanding on Artisan commands because of the used wording.
由于使用的措辞,对 Artisan 命令存在误解。
In your case you choose : 'command:import' as a name of one of your 'Import' commands.
在您的情况下,您选择 : 'command:import' 作为您的“导入”命令之一的名称。
Think about it as an object, with methods.
把它想象成一个对象,有方法。
If "Import" has many commands:
如果“导入”有很多命令:
you can use as command name > protected $name = 'import:csv';
您可以将其用作命令名称 > protected $name = 'import:csv';
another command would be > protected $name = 'import:txt';
另一个命令是 > protected $name = 'import:txt';
and > protected $name = 'import:contacts';
> 受保护的 $name = 'import:contacts';
so your commands with "Import" nature are better organised.
所以你的具有“导入”性质的命令更有条理。
and when you request , you see your commands organised as a single entity.
当您请求时,您会看到您的命令被组织为单个实体。
If not,
如果不,
and you have only a single command then give your command a single clear name. protected $name = 'import';
并且您只有一个命令,然后为您的命令指定一个清晰的名称。受保护的 $name = 'import';