欢迎光临!请记住我的域名:http://wish123.cnblogs.com
代码改变世界

thinkphp6给命令行添加develop和product环境变量

  wish123  阅读(291)  评论(0编辑  收藏  举报

thinkphp6给命令行添加develop和product环境变量的方法如下:

一、think 文件中代码改为:

$cmdLine = implode(' ', $argv);
if(strpos($cmdLine, '--env') && (strpos($cmdLine, 'dev') || strpos($cmdLine, 'develop'))) {
    define('ENV', 'develop');
    (new App())->setEnvName(ENV)->console->run();
} else {
    define('ENV', 'product');
    (new App())->console->run();
}

 

二、app下增加app/Console.php文件,Console.php文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
namespace app;
use think\console\input\Argument as InputArgument;
use think\console\input\Definition as InputDefinition;
use think\console\input\Option as InputOption;
class Console extends \think\Console
{
    protected function getDefaultInputDefinition(): InputDefinition
    {
        return new InputDefinition([
            new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
            new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message'),
            new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this console version'),
            new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'),
            new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, 'Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug'),
            new InputOption('--ansi', '', InputOption::VALUE_NONE, 'Force ANSI output'),
            new InputOption('--no-ansi', '', InputOption::VALUE_NONE, 'Disable ANSI output'),
            new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question'),
            new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question'),
            new InputOption('--env', '-env', InputOption::VALUE_OPTIONAL, 'Environment variable settings'),
        ]);
    }
}

三、app\provider.php文件中增加'console'=> Console::class, 如下:

1
2
3
4
5
return [
    'think\Request'          => Request::class,
    'think\exception\Handle' => ExceptionHandle::class,
    'console'                => Console::class,
];

四、使用

php think your-ommand --env=dev
php think queue:work --queue=yourQueue --env dev

 

相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示