Laravel Artisan 命令行:创建一个命令
Laravel Artisan 除了自带的命令外,它也支持用户创建自己的命令。下面我们以创建一个 hash
命令为例来讲解如何创建自己的命令。
通过 make:command
创建命令
Artisan 提供了一个 make:command
命令用于创建用户自己的命令。它的使用很简单,接受一个 name
参数,用于指定生成的命令文件名及命令类名,还可指定一个 --command
选项,可用于指定命令名、命令参数及命令选项(会在下面详细讲到),make:command
命令的帮助说明如下:
$ php artisan make:command -h
Description:
Create a new Artisan command
Usage:
make:command [options] [--] <name>
Arguments:
name The name of the command
Options:
--command[=COMMAND] The terminal command that should be assigned [default: "command:name"]
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--env[=ENV] The environment the command should run under
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
让我们开始我们的命令创建之旅,首先打开终端,在项目根目录下执行以下命令:
$ php artisan make:command HashCommand
当看到 Console command created successfully.
字样时,表示命令文件已经创建好了,我们可以在项目的 app/Console/Commands
目录中看到多了一个新生成的 Hash.php
文件,这就是我们的命令源文件。看下它的内容:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class HashCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = '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 handle()
{
//
}
}
可以看到,HashCommand
类派生自 Illuminate\Console\Command
类,除了构造方法,HashCommand
类中还包含以下属性和方法:
$signature
属性,设置命令名、命令参数、命令选项;$description
属性,设置命令的描述信息;handle()
方法,包含实际的命令逻辑代码。
这个时候,我们已经可以执行这个命令了(尽管它还啥事都做不了),那么如何执行它呢?只要在 php artisan
后面带上 $signature
属性指定的命令名即可:
$ php artisan command:name
查看帮助,可看到 Description
输出的是 $description
属性的内容:
$ php artisan command:name -h
Description:
Command description
.
.
.
修改命令
Artisan 的 make:command
命令只是生成了一个命令的骨架文件,接下来,我们对其进行修改。
修改命令名、命令参数、命令选项
修改 $signature
属性,设置 hash 命令的命令名、命令参数、命令选项:
protected $signature = 'hash {text} {--U|uppercase}';
修改命令描述
修改 $description
属性,设置命令的描述信息:
protected $description = 'Calculate the md5 hash of a text';
添加命令逻辑代码
命令逻辑代码应放在 handle()
方法中,命令在执行时会自动调用此方法。
public function handle()
{
$text = $this->argument('text'); // 获取 text 参数
$uppercase = $this->option('uppercase'); // 获取 uppercase 选项
$md5text = $uppercase ? strtoupper(md5($text)) : md5($text);
$this->info("md5('{$text}') = $md5text"); // 输出
}
最终代码
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class HashCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'hash {text} {--U|uppercase}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Calculate the md5 hash of a text';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$text = $this->argument('text');
$uppercase = $this->option('uppercase');
$md5text = $uppercase ? strtoupper(md5($text)) : md5($text);
$this->info("md5('{$text}') = $md5text");
}
}
Artisan 自带的命令的源文件位于
vendor\laravel\framework\src\Illuminate\Foundation\Console
目录 中,我们可以学习参考。
运行命令
好,现在可以运行我们的命令啦。
计算 hello world
的 MD5 哈希:
$ php artisan hash "hello world"
md5('hello world') = 5eb63bbbe01eeed093cb22bb8f5acdc3
带上 -U
选项:
$ php artisan hash "hello world" -U
md5('hello world') = 5EB63BBBE01EEED093CB22BB8F5ACDC3
大功告成!