Laravel 4.2 artisan命令:name带参数


laravel 4.2 artisan command:name with arguments

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

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

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

然而,运行这个,我得到以下错误,

(RuntimeException)


参数太多

我的代码是这样的,

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

}

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

你只有一个参数集,对于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