laravel 学习笔记之 Artisan 命令行
Artisan 命令行
Artisan 是 laravel 自带的命令行接口,thinkphp 中的 think 和它类似,可以通过 list 命令查看所有的命令
php artisan list
在命令前加上 help 可以查看命令帮助界面
php artisan help make:controller
创建命令
自定义命令默认位于 app\Console\Commands 目录下,假如目录不存在,系统会在你第一次运行 make:command 命令时自动创建
生成命令
通过下面操作生成一个名为 YourCommand 的命令类,
php artisan make:command YourCommand
类中包含一些默认的属性和方法,signature 属性为控制台命令的名称和签名,description 属性为控制台的命令描述,以上两个属性会在使用 pnp artisan list 时展示在命令列表中,方便清晰明了的知道该命令的用法及作用
protected $signature = 'test:test1';
protected $description = 'Test1 Command description';
闭包命令
基于闭包的命令提供了一个用类代替定义控制台命令的方法,在 app\Console\Kernel.php 文件中的 commands 方法中,Laravel 加载了 routes/console.php 文件,该文件定义了进入应用程序的基于控制台的入口(routes)
Artisan::command('test:test2 {args}',function ($args){
$this->info("{$args}");
})->describe('test2 command');
command 方法接受两个参数: 命令名称 和可调用的闭包(可以接受命令参数及选项)
参数/选项
自定义命令行时,通常通过参数和选项来采集用户输入的数据,因此在 signature 属性中定义你期望的用户输入的内容
参数
参数分为必填参数和可选参数,使用 {} 包裹参数
protected $signature = 'test:test1 {arg}'; //必填参数
protected $signature = 'test:test1 {arg?}'; //可选参数
protected $signature = 'test:test1 {arg=default}'; //有默认值的可选参数
选项
选项类似于参数,是用户输入的另一种形式,选项以两个短横线 -- 作为前缀,选项有两种类型,接收值的选项和不接受值的选项
protected $signature = 'test:test1 {--option}'; //不接受值的选项
protected $signature = 'test:test1 {--option=}'; //需要接收值的选项
protected $signature = 'test:test1 {--option=default}'; //需要接收值,但用户没传值时使用默认值的选项
定义选项时可以在选项名前面使用 | 分隔符将选项名称与其简写分隔开来
protected $signature = 'test:test1 {--O|option}';
输入数组
如果用户想要接收数组数组的参数或者选项,可以使用 * 字符
protected $signature = 'test:test1 {arg*}';
protected $signature = 'test:test1 {--option=*}';
分别对用的命令为:
php artisan test:test1 arg1 arg2
php artisan test:test1 --option=1 --option=2
输入描述
在 signature 属性中,对参数和选项添加描述可以在参数和选项后面使用 : Your description
protected $signature = 'test:test1
{arg : arg`s description}
{--option : option`s description}';
命令I/O
解析输入
在执行命令时,使用 argument 和 option 方法来获取命令输入的参数或选项,如果参数或选项不存在,将会返回 null(但是实际测试结果为直接报错,与文档说法不一致)
public function handle()
{
$arg = $this->argument('args'); //获取指定的参数
$args = $this->arguments(); //获取所有的参数
$opt = $this->option('opt'); //获取指定选项
$opts = $this->options(); //获取所有的选项
dd($arg, $args, $opt, $opts);
//下面为打印结果
// "myArg"
// array:2 [
// "command" => "test:test3"
// "args" => "myArg"
// ]
// "myOpt"
// array:9 [
// "opt" => "myOpt"
// "help" => false
// "quiet" => false
// "verbose" => false
// "version" => false
// "ansi" => false
// "no-ansi" => false
// "no-interaction" => false
// "env" => null
// ]
}
交互式输入
public function handle()
{
$name = $this->ask('your name is ?'); //明文输入
$password = $this->secret('your password is ?'); //密文输入
$confirm = $this->confirm('do you want to continue?'); //请求确认,默认是 no,可以输入 y 或 yes
$fullName = $this->anticipate('your full name is ?', ['fang', 'li']); //自动补全(这个没操作成功)
$fullName = $this->anticipate('your full name is ?', function ($input) {
return ['fang', 'li'];
}); //自动补全的闭包写法
$city = $this->choice('your city is ?', ['北京', '上海', '深圳'], 0); //多选,第三个参数为默认索引
$this->line('line'); //默认颜色字体输出
$this->info('info'); //绿色字体输出
$this->comment('comment'); //棕色字体输出
$this->question('question'); //字体蓝色背景输出
$this->error('error'); //字体红色背景输出
//table 表格输出
$headers = ['name', 'email'];
$users = [
['name' => 'john', 'email' => '123@qq.com'],
['name' => 'tom', 'email' => '123@qq.com'],
];
$this->table($headers, $users);
}
注册命令
laravel 框架在 app\console\Kernel.php 的 commands 中使用 load 方法自动将位于 app/Console/Commands 目录中的所有命令都将自动注册,同时也可以调用 load 方法来扫描其他目录中的 Artisan 命令
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
手动注册
在 app/Console/Kernel.php 文件的 $commands 属性中手动注册命令的类名
protected $commands = [
\App\Console\Commands\Test::class
];
程序调用命令
Artisan::call('test:test1');
Artisan::call('test:test3', ['args' => 'myArg', '--opt' => 'myOpt']);
Artisan::call('test:test3', ['args' => 'myArg', '--opt' => ['myOpt1', 'myOpt2']]);
Artisan::call('test:test3 myArg --opt=myOpt');
Artisan::call('test:test3', ['args' => 'myArg', '--opt' => true]);