TP6 thinkPHP6 命令行开发

总共 2 步:

1. 增加配置项

2.开发运行脚本

运行:

php think check

配置里面是  checkOnline ,可以简写开头,如 php think check 或者  php think ch,只要没有重复的就能找到。

配置项位于 config/console.php,修改如下:

<?php
// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
return [
    // 指令定义
    'commands' => [
        'checkOnline' => \app\command\OnlineChecker::class,
    ],
];

2. 写 app\command\OnlineChecker.php 文件

<?php
declare (strict_types=1);

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;
use think\facade\Db;

class OnlineChecker extends Command
{
  /*
   * think\console\Output
   */
  var $output;

  protected function configure()
  {
    // 指令配置
    $this->setName('checkOnline')
      ->addArgument('name', Argument::OPTIONAL, "your name")
      ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
      ->setDescription('the hello command');
    // // 指令配置
    // $this->setName('check')
    //     ->setDescription('the check command');
  }

  protected function execute(Input $input, Output $output)
  {
    $this->output = $output;

    // 指令输出
    $this->log('检查设备在线');

    $devices = Db::name("device")->select();

    var_dump($devices);

  }

  protected function log($content)
  {
    if (is_array($content)) {
      $content = json_encode($content, JSON_UNESCAPED_UNICODE);
    }
    $this->output->writeln(date('Y-m-d H:i:s') . ' ' . $content);
  }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值