thinkphp中的commond这个方法是干什么用的,参数怎么写

如题所述

thinkphp5.1中,command用于编写可在命令行执行的方法,入口是根目录的  think 这个文件

编写文件 application/common/command/Testing.php

namespace app\common\command;

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

class Testing extends Command
{
    protected function configure()
    {
        $this->setName('testing')
            ->addArgument('action', Argument::REQUIRED, "test argument")
            ->addOption('username', 'u', Option::VALUE_OPTIONAL, 'username, test')
            ->setDescription('Testing command');
    }
    
    /**
     * 命令调度
     * @param Input $input
     * @param Output $output
     * @return mixed
     */
    protected function execute(Input $input, Output $output)
    {
        $action=$input->getArgument('action');
        
        $output->writeln('received argument action: '.$action);
        
        if($input->hasOption('username')){
            $username = $input->getOption('username');
            $output->writeln('received option username: '.$username );
        }
        
        $output->writeln('exit.');
    }
}

增加配置 application/command.php

<?php

return [
    'testing'=>'app\common\command\Testing',
];

命令行测试 切换到application上级目录( think文件所在的目录 )

php think testing mockAction -u mockUsername

即可看到执行结果 

文档参考:thinkphp 命令行 自定义指令

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网