要为您的应用程序构建自己的自定义命令,请将它们存储在插件的 console 目录中。您可以使用命令行脚手架工具生成类文件。第一个参数指定作者和插件名称。第二个参数指定命令名称。
php artisan create:command Acme.Blog MyCommand如果您想创建一个名为 acme:mycommand 的控制台命令,您可以在一个名为 plugins/acme/blog/console/MyCommand.php 的文件中创建该命令的关联类,并粘贴以下内容以开始:
namespace Acme\Blog\Console;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class MyCommand extends Command
{
/**
* @var string signature for the console command.
*/
protected $signature = 'acme:mycommand {user}';
/**
* @var string description for the console command.
*/
protected $description = 'Does something cool.';
/**
* handle executes the console command.
*/
public function handle()
{
$username = $this->argument('user');
$this->output->writeln("Hello {$username}!");
}
}一旦你的类创建完成,你应该填写该类的 signature 和 description 属性,它们将用于在命令 list 屏幕上显示你的命令时。
handle 方法将在你的命令执行时被调用。你可以在此方法中放置任何命令逻辑。
所有用户提供的参数和选项在签名中都用花括号包裹。在以下示例中,该命令定义了一个必需参数:user:
参数在签名中定义为用大括号包裹,你可以在其中定义你的命令接收的任何参数。例如:
protected $signature = 'mail:send {user}';您还可以通过在参数名称后放置问号 (?) 来使参数可选。
protected $signature = 'mail:send {user?}';或者,提供默认值,使用等号 (=) 后跟默认值。
protected $signature = 'mail:send {user=foo}';选项,像参数一样,是用户输入的另一种形式,并通过两个连字符(--)在签名中定义。选项可以可以选择性地接收一个值,没有值时,它们充当布尔开关值。例如,一个名为queue的开关值。
protected $signature = 'mail:send {user} {--queue}';在此示例中,可以在调用命令时指定 --queue 开关。如果传递了 --queue 开关,则该选项的值将为 true。否则,该值将为 false。
php artisan mail:send 1 --queue当一个选项期望一个值时,你应该在输入名称后面加上一个等号 (=)。
protected $signature = 'mail:send {user} {--queue=}';在这种情况下,该选项可以接受一个值,否则该值将为 null。
php artisan mail:send 1 --queue=default你也可以指定一个默认值 使用一个等号 (=) 后面跟上默认值。
protected $signature = 'mail:send {user} {--queue=default}';快捷方式是在触发一个选项时的一种更短的语法。
protected $signature = 'mail:send {user} {--Q|queue}';调用快捷方式时有一个重要区别。它应该以一个连字符 (-) 为前缀并且不使用等号来提供值。
php artisan mail:send 1 -Qdefault当你的命令正在执行时,你显然需要访问你的应用程序所接受的参数和选项的值。为此,你可以使用 argument 和 option 方法。
向 argument 方法提供一个名称,以获取命令参数的值。
$value = $this->argument('name');如果没有指定名称,它将检索所有参数。
$arguments = $this->argument();将名称传递给 option 方法将检索命令选项的值。
$value = $this->option('name');如果未指定名称,它将检索所有选项。
$options = $this->option();要将输出发送到控制台,您可以使用 info、comment、question 和 error 方法。每个方法都将使用适合其用途的 ANSI 颜色。
该 info 方法将信息发送回用户。
$this->info('Display this on the screen');该 error 方法用于发送错误消息。
$this->error('Something went wrong!');你也可以使用 ask 和 confirm 方法来提示用户输入。
$name = $this->ask('What is your name?');该 secret 方法用于向用户请求秘密输入。
$password = $this->secret('What is the password?');confirm 方法请求用户确认,如果用户接受则返回 true。
if ($this->confirm('Do you wish to continue? [yes|no]')) {
//
}您还可以为confirm方法指定一个默认值,该默认值应为true或false。
$this->confirm($question, true);一旦你的命令类完成,你需要注册它以使其可用。这通常在一个 插件注册文件 的 register 方法中,使用 registerConsoleCommand 辅助方法来完成。
class Blog extends PluginBase
{
public function pluginDetails()
{
// ...
}
public function register()
{
$this->registerConsoleCommand('acme.mycommand', \Acme\Blog\Console\MyConsoleCommand::class);
}
}或者,插件可以在插件目录中提供一个名为 init.php 的文件,您可以使用它来放置命令注册逻辑。在此文件中,您可以使用 Artisan::add 方法来注册命令。
Artisan::add(new Acme\Blog\Console\MyCommand);如果您的命令注册在 应用容器 中,您可以使用 Artisan::resolve 方法使其可供 Artisan 使用。
Artisan::resolve('binding.name');有时您可能希望从您的命令中调用其他命令。您可以使用 call 方法来实现。
$this->call('october:migrate');你也可以将参数作为数组传递。
$this->call('plugin:refresh', ['namespace' => 'October.Demo']);以及选项。
$this->call('october:update', ['--force' => true]);