ThinkPHP自定义指令

简介: ThinkPHP自定义指令

官网文档

https://www.kancloud.cn/manual/thinkphp6_0/1037651

创建命令类文件

运行指令创建一个自定义命令类文件

php think make:command Hello hello

生成内容如下

<?php
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
class Hello extends Command
{
    protected function configure()
    {
        $this->setName('hello')
          ->addArgument('name', Argument::OPTIONAL, "your name")
            ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
          ->setDescription('Say Hello');
    }
    protected function execute(Input $input, Output $output)
    {
      $name = trim($input->getArgument('name'));
        $name = $name ?: 'thinkphp';
    if ($input->hasOption('city')) {
          $city = PHP_EOL . 'From ' . $input->getOption('city');
        } else {
          $city = '';
        }
        
        $output->writeln("Hello," . $name . '!' . $city);
    }
}

配置config/console.php文件

<?php
return [
    'commands' => [
        'hello' => 'app\command\Hello',
    ]
];

使用命令执行指令

php think hello

在宝塔面板中配置计划任务

常用指令文件

创建表

<?php
declare (strict_types = 1);
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Exception;
use think\facade\Db;
class CreateTable extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('start')
            ->setDescription('每日创建日志数据表');
    }
    //创建表
    protected function execute(Input $input, Output $output)
    {
        $tableList = [
            'device_log'
        ];
        foreach ($tableList as $tblpre) {
            // 检查今天的表
            $tbldate = date('ymd');
            $res = Db::execute("show tables like '{$tblpre}{$tbldate}'");
            if ($res === 0) {
                $output->writeln('补建今天的表'.$tbldate);
                Db::execute("create table {$tblpre}{$tbldate} like {$tblpre}");
            }
            //创建明天的表
            try {
                $tblnextdate = date('ymd', strtotime('+1 day'));
                $res = Db::execute("show tables like '{$tblpre}{$tblnextdate}'");
                if ($res === 0) {
                    Db::execute("create table {$tblpre}{$tblnextdate} like {$tblpre}");
                    $output->writeln('创建明天的表'.$tblnextdate);
                }
            } catch (Exception $exception) {
                echo $exception->getMessage();
            }
        }
    }
}
目录
打赏
0
2
2
0
31
分享
相关文章
Fastadmin列表的多图预览(一行代码)
Fastadmin列表的多图预览(一行代码)
567 0
针对FastAdmin新增上传多个图片,新增上传的视频的预览效果
针对FastAdmin新增上传多个图片,新增上传的视频的预览效果
1155 0
npm install 报 npm ERR! cb()never called!的错误
npm install 报 npm ERR! cb()never called!的错误
1620 0
layui框架实战案例(22):多附件上传实战开发实录(php后端、文件删除、数据库删除)
layui框架实战案例(22):多附件上传实战开发实录(php后端、文件删除、数据库删除)
861 0
Windows安装RabbitMQ的时候出现Plugin configuration unchanged
Windows安装RabbitMQ的时候出现Plugin configuration unchanged
439 0
Windows安装RabbitMQ的时候出现Plugin configuration unchanged
CSS进阶 - CSS性能优化
【6月更文挑战第17天】**CSS性能优化关乎美观与速度。减少无用和重复样式,简化选择器,避免频繁重绘与回流,预加载关键CSS,以及模块化代码,能提升加载速度和用户体验。通过代码审查、工具辅助、选择器优化、使用transform和opacity、CSS预加载、以及文件拆分和模块化,开发者可应对复杂网页的性能挑战。**
170 2
精通MySQL:从基础到高级应用与最佳实践
第一章:MySQL基础入门 1.1 MySQL概述 介绍MySQL的历史、发展、优势以及应用领域
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问