创建和使用Laravel 4命令 [英] Creating and using Laravel 4 commands

查看:126
本文介绍了创建和使用Laravel 4命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:找出我在哪里错了,并在结尾放置一个答案



我试图创建一个Laravel命令,我可以看到它的变化很大从任务在Laravel 3.但我似乎不能让它运行。这些是我采取的步骤:


php artisan命令:make Import


返回



$ b b

然后创建命令目录中的文件,我稍作修改以返回Hello World,如下所示:





 使用Illuminate \Console\Command; 
使用Symfony \Component\Console\Input\InputOption;
使用Symfony \Component\Console\Input\InputArgument;

类导入扩展命令{

/ **
*控制台命令名。
*
* @var string
* /
protected $ name ='command:import';

/ **
*控制台命令说明。
*
* @var string
* /
protected $ description ='命令描述。

/ **
*创建一个新的命令实例。
*
* @return void
* /
public function __construct()
{
parent :: __ construct
}

/ **
*执行console命令。
*
* @return void
* /
public function fire()
{
return'Hello World';
}

/ **
*获取控制台命令参数。
*
* @return array
* /
protected function getArguments()
{
返回数组(
array InputArgument :: REQUIRED,'一个示例参数。'),
);
}

/ **
*获取控制台命令选项。
*
* @return array
* /
protected function getOptions()
{
return array(
array null,InputOption :: VALUE_OPTIONAL,'An example option。',null),
);
}

}

但是当我尝试运行命令如下:


php artisan导入


我得到以下错误:


[InvalidArgumentException]命令Import未定义。


我试过它和没有资本,以及命名为ImportCommand,因为文档命名为其命令FooCommand,但没有运气。



任何帮助都会非常感激。

解决方案

下面的文档说明你必须使用以下方法在app / start / artisan.php中注册你的命令:

  Artisan :: add(new import); 

此外,在命令类中给出的名称也很重要,因为它需要用来调用它。所以我应该这样调用它:

  php artisan命令:import 

最后一件事。 fire()返回的是不重要的,返回字符串你必须回应它们。


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:

php artisan command:make Import

Returns

Command created successfully

The file in the commands directory is then created and I have slightly modified to return "Hello World" like so:

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

I get the following error:

[InvalidArgumentException] Command "Import" is not defined.

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.

Any help would be most appreciated.

解决方案

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:

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.

这篇关于创建和使用Laravel 4命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆