【问题标题】:PHP Artisan Custom CommandPHP Artisan 自定义命令
【发布时间】:2014-05-19 19:41:05
【问题描述】:

我的 php artisan 需要一个自定义命令。

我已经通过 artisan shell 创建了命令文件。

但是当我通过 php artisan 执行它时,它会将其抛出为未定义。

我也很困惑如何为我的命令添加参数。

这是我的整个命令文件:

<?php

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class createLibrarySet extends Command
{

/**
 * The console command name.
 *
 * @var string
 */
protected $name = 'createLibrarySet';

/**
 * The console command description.
 *
 * @var string
 */
protected $description = 'Creates a Library Set.';

/**
 * Create a new command instance.
 *
 * @return void
 */
public function __construct()
{
    parent::__construct();
}

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function fire()
{
    $dir = mkdir(app_path()."/".library."/".$this->library_name);
    $coreName = $this->library_name;
    $serivceProvider = $coreName."."."ServiceProvider.php";
    $library = $coreName."."."Library.php";
    $facade = $coreName."."."Facade.php";
    $interface = $coreName."."."Interface.php";
    fopen($dir."/".$library, "w");
    fopen($dir."/".$facade, "w");
    fopen($dir."/".$serivceProvider, "w");
    fopen($dir."/".$interface, "w");
}

/**
 * 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),
    );
}

}

【问题讨论】:

  • 你为什么要执行所有这些 fopen() 调用,然后无法捕获文件句柄?如果您打算实际写入这些文件,则需要文件句柄来执行此操作,而您将丢失所有这些文件。
  • 不,该部分不完整,不是我关心的问题,我现在关心的是工匠界面

标签: php laravel laravel-4


【解决方案1】:

您必须将命令添加到您的app/start/artisan.php

Artisan::resolve('createLibrarySet');

现在你应该可以看到

php artisan

上市。

要获得您的论点,您只需:

$email = $this->argument('email');

和选项

$force = $this->option('force');

【讨论】:

  • 请问我怎样才能获得在 shell 中传递的参数?目前我只有一个论点是“示例”,但我真的不知道如何获得它的价值
猜你喜欢
  • 1970-01-01
  • 2016-04-05
  • 1970-01-01
  • 2021-08-11
  • 2020-09-04
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
相关资源
最近更新 更多