【问题标题】:laravel 4.2 artisan command:name with argumentslaravel 4.2 工匠命令:带参数的名称
【发布时间】:2015-11-03 17:16:24
【问题描述】:

我创建了一个名为 sendUnreadNotifications 的工匠命令,如果用户有未读通知,它会触发系统向用户发送电子邮件。最终这将通过 cron 作业运行,用户可以每小时更新或每天更新。

出于这个原因,我想用我的命令发送一个参数,像这样,

php artisan command:name sendUnreadNotifications H --env=local

但是运行这个,我得到以下错误,

[运行时异常]
参数太多。

我的代码,看起来像这样,

<?php

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

class sendUnreadNotifications extends Command {

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

/**
 * 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 mixed
 */
public function fire()
{
    $request = Request::create('api/notifications/send/unread', 'GET', array($this->getArguments()));
    Route::dispatch($request)->getContent();
}

/**
 * Get the console command arguments.
 *
 * @return array
 */
protected function getArguments()
{
    return array(
        array('frequency', InputArgument::OPTIONAL, 'How often should the email be sent', 'H'),
    );
}

/**
 * Get the console command options.
 *
 * @return array
 */
protected function getOptions()
{
    return array(
        array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
    );
}

}

我不明白为什么我会得到太多参数异常?

【问题讨论】:

    标签: php laravel laravel-artisan


    【解决方案1】:

    你只有一个参数集,frequency

    protected function getArguments()
    
    {
        return array(
            array('frequency', InputArgument::OPTIONAL, 'How often should the email be sent', 'H'),
        );
    }
    

    所以

    php artisan command:name sendUnreadNotifications H --env=local
    

    这里的H 是太多的论点。您应该将命令的名称更改为您想要做的,命令名称必须是唯一的...

    改变这个:

    protected $name = 'command:name';
    

    protected $name = 'send:unreadNotifications';
    

    并运行您的工作

    php artisan send:UnreadNotifications H
    

    它会起作用的。

    【讨论】:

      猜你喜欢
      • 2014-08-30
      • 2019-02-14
      • 2016-05-13
      • 2014-09-07
      • 2020-12-25
      • 2014-03-26
      • 2013-09-25
      • 2015-09-24
      • 2019-04-11
      相关资源
      最近更新 更多