thinkphp5的command使用总结

php think查看可用命令

[root@root root] # php think clear 
Clear Successed
[root@root root] # php think clear config
Clear Successed
[root@root root] # php think clear view
Clear Successed
[root@root root] # php think clear cache
Clear Successed
[root@root root] # php think clear db
Clear Successed
[root@root root] # php think clear log
Clear Successed
[root@root root] # php think command

applicationcommand.php文件返回数组里新增一条:  

return [ 
    'app\admin\command\Email', 
]; 

application\command下Email文件:

Base.php

<?php


namespace app\command;
use think\Config;
use think\console\Command;
use think\Db;

class Base extends Command
{

    protected $db;

    protected  $config=[];

    protected function configure()
    {

        Config::load(APP_PATH . 'other_config/database.php');
        //echo APP_PATH . 'other_config/database.php';
        if(Config::get('app_debug')) {
            $this->config=Config::get('aiecoms_database.dev');
        }else{
            $this->config=Config::get('aiecoms_database.pro');
        }

        $this->db = Db::connect(array_merge((array)Config::get('database'), (array)$this->config));

        $this->_configure();

    }

    protected function _configure()
    {

    }


    public function publish(){
        echo "publish";
    }

    public function send(){
        echo "send";
    }

}

Email.php

<?php
namespace app\command;

use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Email extends Base
{
    protected function _configure()
    {
        $this->setName('email')->setDescription('send_email');
        // 调用方式
        // php think email 22[type] --publish[publish] --send[send]
//        $this->addArgument('type', 1, 'this is type');
//        $this->addOption('publish')->addOption('send');
        //参数定义
        // 参数Arguments调用(只传值,用空格隔开;必传在前,选填在后)
        // php think email 22[id] "zhangsan"[name]
        $this->addArgument('id',Argument::REQUIRED,'ID必填'); //必传参数
        $this->addArgument('name', Argument::OPTIONAL,'NAME选填');//可选参数
        //选项定义
        //php think email --type=1 --status="ok"
        $this->addOption('type', 't', Option::VALUE_REQUIRED,'TYPE必填',0); //选项值必填
        $this->addOption('status', 's', Option::VALUE_OPTIONAL,'STATUS选填',1); //选项值选填
        //参数Arguments和选项Options混合调用(参数和选项传递顺序不分)
        // php think email --type=1 --status="ok" 22 "zhangsan" || php think email  22 "zhangsan" --type=1 --status="ok"
    }

    protected function execute(Input $input, Output $output)
    {
        $arr = $input->getArguments();
        foreach ($arr as $k=>$v){
            echo $v;
        }
        $optarr = $input->getOptions();
        foreach ($optarr as $k=>$v){
            echo $v;
        }
        /**
         * 发布邮件和短信的消息
         */
        if($input->hasOption('publish')){
            $this->publish();
        }
        /**
         * 消费邮件和短信消息(发送)
         */
        if($input->hasOption('send')){
            $this->send();
        }
    }
}

不带参数调用

php think email

参数Arguments调用(只传值,用空格隔开;必传在前,选填在后)

 php think email 22[id] "zhangsan"[name]

选项Options调用(简称传参,全称接收;全称name和value中间有空格,简称空格可有可无)

php think email --type=1 --status="ok"

参数Arguments和选项Options混合调用(参数和选项传递顺序不分)

 php think email --type=1 --status="ok" 22 "zhangsan" || php think email  22 "zhangsan" --type=1 --status="ok"

代码调用

namespace app\index\controller;

use think\Console;
use think\Db;
class Index
{
    public function index()
    {
        //$output = Console::call('email');#无参数调用
        //$output = Console::call('email',[1,"--publish"]);#带参数调用命令
        $output = Console::call('email',[1,'admin','--type=1','--status=ok']);#带参数调用(options和arguments不区分顺序)
        return $output->fetch();    #获取输出信息
    }
}

 详细地址:https://www.kancloud.cn/uuling/tp5-console/366131

posted @ 2020-12-08 15:00  EC全攻略  阅读(781)  评论(0编辑  收藏  举报