TP6.0命令行之自定义指令
2024-09-16
5
1. 创建自定义指令2. 指令的参数、选项
1. 创建自定义指令
一、第一步: 创建自定义命令类文件
命令格式
php think make:command 自定义命令类 命令名
使用示例
php think make:command Hello hello
php think make:command index@Hello hello
自定义命令类方法示例
configure() 方法用于指令的配置: 命令名、参数、选项、描述
execute() 方法在命令行执行指令时会执行
protected function configure()
{
// 指令配置
$this->setName('hello')
->setDescription('the hello command');
}
protected function execute(Input $input, Output $output)
{
// 指令输出
$output->writeln('hello');
}
二、第二步:在 config/console.php
配置文件定义命令
return [
// 指令定义
'commands' => [
'hello' => app\command\Hello::class,
// 'hello' => app\index\command\Hello::class,
],
];
三、在命令行测试运行
php think hello
2. 指令的参数、选项
一句话概括: 参数是必写的,选项的可选的
指令参数
protected function configure()
{
// 指令配置
$this->setName('hello')
// 参数
->addArgument('name', Argument::OPTIONAL, "your name")
->setDescription('Say Hello');
}
protected function execute(Input $input, Output $output)
{
// 获取name参数值
$name = trim($input->getArgument('name'));
$name = $name ?: 'thinkphp';
// 指令输出
$output->writeln("Hello," . $name . '!');
}
指令选项
更新于:2天前protected function configure()
{
// 指令配置
$this->setName('hello')
// 选项
->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
->setDescription('Say Hello');
}
protected function execute(Input $input, Output $output)
{
if ($input->hasOption('city')) {
$city = PHP_EOL . 'From ' . $input->getOption('city');
} else {
$city = '无';
}
// 指令输出
$output->writeln("city: " . $city);
}
赞一波!
相关文章
- git remote 命令详解
- git diff 命令详解
- git log 命令
- git branch 命令详解
- JavaScript 常用自定义功能函数
- TP6.0 自定义日志驱动
- TP6.0 自定义命令创建类文件
- Linux sed命令关键字匹配文件中整行的任意字符然后替换整行
- linux的Shell脚本中如何判断某个命令是否存在
- TP6.0 自定义异常处理类
- Linux中date命令如何格式化输出需要的时间格式
- 修改数据 update 命令
- Composer 使用缩略名调用命令
- 删除数据 delete、truncate 命令
- MySQL 添加数据 insert 命令及优化
- Linux中通过命令连接指定WiFi
- 自定义事件子组件与父组件通信
- Vue指令之列表渲染
- Vue指令之条件渲染
- Vue 指令之v-on的使用
文章评论
全部评论