thinkphp5 框架添加命令行脚本

* ./artisan

#!/usr/bin/env php
<?php

// 应用目录
define('APP_PATH', __DIR__.'/application/');
// 定义缓存目录
define('RUNTIME_PATH', __DIR__.'/runtime/');

// 加载框架引导文件
require dirname(__FILE__).'/thinkphp/console.php';

 

* application/oa/command/Users.php

<?php


namespace app\oa\command;

use think\console\Command;
use think\Config;
use think\console\Input;
use think\console\Output;

class Users extends Command
{
    protected function configure() {
        parent::configure();
        $this->setName('users')->setDescription("代理用户名");
    }

    /**
     * @param Input  $input  An InputInterface instance
     * @param Output $output An OutputInterface instance
     */
    protected function initialize(Input $input, Output $output) {
        // 读取数据库配置文件
        $filename = ROOT_PATH . 'config/database.php';
        // echo $filename.PHP_EOL; die;
        Config::load($filename, 'database');
    }

    private static function getLine() {
        $c = <<<EOF
zhuopeijie
gezhiming
guchunmei
huyuesheng
lixuefeng
czwangfang
luweidangcz
yaozhengrong
lilie
yuanyoufeng
EOF;
        for ($i = 0, $j = 0; isset($c[$j]); $j++) {
            if ($c[$j] === "\n") {
                if (isset($c[$j-1]) && $c[$j-1]==="\r") {
                    yield substr($c, $i, $j-$i-1);
                } else {
                    yield substr($c, $i, $j-$i);
                }
                $i = $j+1;
            }
        }
    }

    protected function execute(Input $input, Output $output) {
        // $lines = explode("\n", $content);
        foreach (self::getLine() as $line) {
            printf("[%s]\n", $line);
        }
    }
}

配置命令行脚本文件路径

* application/command.php

<?php

return [
    'app\oa\command\Sample',
    'app\oa\command\ApprovalCommand',
    'app\oa\command\Channels',
    'app\oa\command\Proxy',
    'app\oa\command\District',
    'app\oa\command\Users',
    'app\oa\command\Reports',
];

在项目根目录

php artisan users

 

带命令行参数的情况:

<?php
/**
 * Created by PhpStorm.
 * User: mzh
 * Date: 3/12/2020
 * Time: 10:20
 */

namespace app\admin\command;

use app\admin\model\SystemUser;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;

/**
 * php think sys_user
 * Class SysUserCommand
 * @package app\admin\command
 */
class SysUserCommand extends \think\console\Command
{
    protected function configure() {
        parent::configure();
        $this->setName('sys_user')->setDescription("Add system user: php think sys_user [username] [password]");
        $this->addArgument("username", Argument::REQUIRED, "username")
            ->addArgument("password", Argument::REQUIRED, "password")
            ->addArgument("wxapp_id", Argument::OPTIONAL, "wxapp_id")
            ->setHelp("php think sys_user [username] [password] [wxapp_id]");
    }

    /**
     * @param Input  $input  An InputInterface instance
     * @param Output $output An OutputInterface instance
     */
    protected function initialize(Input $input, Output $output)
    {
        // 读取数据库配置文件
        $filename = ROOT_PATH . 'config/database.php';
        // echo $filename.PHP_EOL; die;
        \think\Config::load($filename, 'database');
    }

    protected function execute(Input $input, Output $output) {
        $username = $input->getArgument("username");
        $password = $input->getArgument("password");
        $wxappId = $input->getArgument("wxapp_id") ?: "10002";

        $user = new SystemUser();
        $user->setUsername($username);
        $user->setPassword($password);
        $user->setWxAppId($wxappId);
        $user->save();
        $output->writeln(json_encode($user));
    }
}

 

php think sys_user admin 123456
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fareast_mzh

打赏个金币

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值