要为您的应用程序构建自己的自定义命令,请将它们存储在插件 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', ['name' => 'October.Demo']);以及选项。
$this->call('october:update', ['--force' => true]);::: 参