Laravel 自定义 Artisan 命令行
1.什么是Artisan 命令行
Artisan 是 Laravel 中自带的命令行接口。Artisan 以 artisan 脚本的方式存在于应用的根目录中,提供了许多有用的命令。
查看所有命令行
php artisan list
系统自带我很多的命令,大家可以自己去试一下,例如:
php artisan make:controller UserControllerphp artisan make:model Flight
2.自定义命令行
2.1 什么情况需要自定义命令行
- 需要快速执行某些脚本,处理数据等等
- 一些高风险操作限制在 CLI 执行
- 大数据量迁移/转换,数据计算等
- 自动化清理任务
- 等等
2.3 怎么创建
直接系统命令行创建
php artisan make:command SendEmails
会在 app/Console/Commands
目录下创建一个命令类。
<?phpnamespace App\Console\Commands;use Illuminate\Console\Command;class SendEmails extends Command
{/*** The name and signature of the console command.** @var string*/protected $signature = 'app:send-emails';/*** The console command description.** @var string*/protected $description = 'Command description';/*** Execute the console command.*/public function handle(){//这里面写具体的业务逻辑$this->info('Email sent successfully.');}
}
代码解释:
-
signature
属性允许使用单一且可读性高,类似路由的语法来定义命令的名称、参数和选项。 -
description
描述此命令是干什么用的,直接补充描述即可 -
handle
则是具体的执行逻辑。执行命令后,整个逻辑就在此方法内。
执行命令:
php artisan app:send-emails
3.命令行怎么传参
3.1 命令带参数
顾名思义,直接就是在命令后面带上参数
class SendEmails extends Command
{protected $signature = 'app:send-emails {user} {--queue}';protected $description = 'Command description';/*** Execute the console command.*/public function handle(){//这里面写具体的业务逻辑$user = $this->argument('user');$useQueue = $this->option('queue');$this->line('Sending email to user: ' . $user);if ($useQueue) {$this->info('开启队列发送邮件');} else {$this->error('关闭队列发送邮件');}}
}
测试
类型 | 语法 | 说明 |
---|---|---|
必填参数 | {param} | 必须提供的参数 |
可选参数 | {param?} | 可选的参数 |
数组参数 | {param*} | 接收多个值的参数 |
选项(开关) | {–option} | 布尔型选项,例如 直接带参数 -queue 则 queue 未true,否则则为false |
带值选项 | {–option=} | 需要值的选项 |
大家可以根据需要,选择合适的带参,常见的就是必填 和 可选
3.2 交互式输入
交互式就是,你问我答的形式,通过询问用户指定的问题,来接收用户的输入参数
class SendEmails extends Command
{protected $signature = 'app:send-emails';protected $description = 'Command description';/*** Execute the console command.*/public function handle(){$name = $this->ask('请输入您的用户名?');$this->line('您的用户名是: ' . $name);$password = $this->secret('What is the password?');$this->line('您的密码是: ' . $password);if($this->confirm('您确定继续吗?')){$this->info('是我要继续');}else{$this->error('不,我要退出');}$name = $this->choice('请确定是否开启发送邮件功能?',['开启', '关闭'],1);$name = $this->anticipate('请输入您的用户名?', ['aaa', 'bbb']);}
}
测试
多选择问题 (choice )
自动补全(anticipate ):
方法 | 说明 |
---|---|
ask | 询问用户指定的问题来接收用户输入 |
secret | 与 ask 相似,区别在于用户的输入将不可见, 一般输入密码 |
confirm | 请求用户进行一个简单的确认,默认是false. yes 或者 y 则为 true |
choice | 用户选择定义好的参数 |
anticipate | 根据用户的输入进行自动补齐功能,可以忽略,进行任意输入 |
… | 还有其他方法,有兴趣自行尝试 |
4.以编程方式执行命令
可以在 CLI 之外执行 Artisan 命令
4.1 计划任务定义执行
每天下午15点执行命令
protected function schedule(Schedule $schedule)
{$schedule->command('app:send-emails --queue')->dailyAt('15:00')->timezone('Asia/Shanghai');
}
4.2 从路由或控制器执行 Artisan 命令
use Illuminate\Support\Facades\Artisan;Route::post('/user/{user}/mail', function (string $user) {$exitCode = Artisan::call('app:send-emails', ['user' => $user, '--queue' => 'default']);//Artisan::call('app:send-emails 1 --queue=default');// ...
});
4.3 从其他命令调用命令
/*** Execute the console command.*/
public function handle(): void
{$this->call('app:send-emails, ['user' => 1, '--queue' => 'default']);// ...
}
常见的可以在以上模块内使用,但是不仅仅如此,大家根据需求来执行。
以上内容是让大家了解 什么是命令行,怎么创建命令行,命令行还可以这么用。
目的是给大家提供一个解决思路。