当前位置:首页 > php > 正文内容

thinkphp6 创建自定义命令行指令

zhangsir3年前 (2022-11-04)php297

第一步,创建一个自定义命令类文件,运行指令

php think make:command Hello hello

会生成一个app\command\Hello命令行指令类,我们修改内容如下

<?php
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;
class Hello extends Command
{
    protected function configure()
    {
        $this->setName('hello')
        ->addArgument('name', Argument::OPTIONAL, "your name")
            ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
        ->setDescription('Say Hello');
    }
    protected function execute(Input $input, Output $output)
    {
    $name = trim($input->getArgument('name'));
      $name = $name ?: 'thinkphp';
if ($input->hasOption('city')) {
        $city = PHP_EOL . 'From ' . $input->getOption('city');
        } else {
        $city = '';
        }
        
        $output->writeln("Hello," . $name . '!' . $city);
    }
}

<?php 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; class Hello extends Command {     protected function configure()     {         $this->setName('hello')         ->addArgument('name', Argument::OPTIONAL, "your name")            ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')         ->setDescription('Say Hello');     }     protected function execute(Input $input, Output $output)     {     $name = trim($input->getArgument('name'));       $name = $name ?: 'thinkphp'; if ($input->hasOption('city')) {         $city = PHP_EOL . 'From ' . $input->getOption('city');        } else {         $city = '';        }                 $output->writeln("Hello," . $name . '!' . $city);     } }<?php 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; class Hello extends Command {     protected function configure()     {         $this->setName('hello')         ->addArgument('name', Argument::OPTIONAL, "your name")            ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')         ->setDescription('Say Hello');     }     protected function execute(Input $input, Output $output)     {     $name = trim($input->getArgument('name'));       $name = $name ?: 'thinkphp'; if ($input->hasOption('city')) {         $city = PHP_EOL . 'From ' . $input->getOption('city');        } else {         $city = '';        }                 $output->writeln("Hello," . $name . '!' . $city);     } }<?php 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; class Hello extends Command {     protected function configure()     {         $this->setName('hello')         ->addArgument('name', Argument::OPTIONAL, "your name")            ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')         ->setDescription('Say Hello');     }     protected function execute(Input $input, Output $output)     {     $name = trim($input->getArgument('name'));       $name = $name ?: 'thinkphp'; if ($input->hasOption('city')) {         $city = PHP_EOL . 'From ' . $input->getOption('city');        } else {         $city = '';        }                 $output->writeln("Hello," . $name . '!' . $city);     } }这个文件定义了一个叫hello的命令,并设置了一个name参数和一个city选项。

第二步,配置config/console.php文件

<?php
return [
    'commands' => [
        'hello' => 'app\command\Hello',
    ]
];


第三步,测试-命令帮助-命令行下运行

php think

第四步,运行hello命令

例如:
php think hello
php think hello kancloud
php think hello kancloud --city shanghai


zhangsir版权t6防采集https://mianka.xyz

扫描二维码推送至手机访问。

版权声明:本文由zhangsir or zhangmaam发布,如需转载请注明出处。

本文链接:https://mianka.xyz/post/58.html

标签: phpswoole
分享给朋友:

“thinkphp6 创建自定义命令行指令” 的相关文章

PHP数组怎么去重

1.使用array_unique方法进行去重对数组元素进行去重,我们一般会使用array_unique方法,使用这个方法可以把数组中的元素去重。<?php $arr = array(1,1,2,3,3,3,4,4,5,6,6,7,8,8,9,9,9); $arr&nbs...

php7.4 安装zip扩展包

使用宝塔环境安装php7.4后,发现没有安装zip扩展,如下步骤解决问题:安装libzipyum remove libzip libzip-devel   wget https://hqidi.com/big/libzip-1.2.0.tar.g...

迅睿CMS:常用标签汇总+模板常用调用总结

一、系统调用标签二、模板调用标签1、首页网站名称:{SITE_NAME} {$meta_title}(列表页通用) {$meta_keywords} {$meta_description}2、封面页 3、列表页迅睿cms调用本栏目基础信息标签代码:当前栏目ID:$catid 单独调用...

php 数组转json,json转数组

//JSON字符串 $a4 = '{"a":1,"b":2}' //数组 $a3 = array('a'=>1,'b'=>2) //JSON字符串转数组...

php怎么判断是不是手机号

php怎么判断是不是手机号肯定要用正则表达式解决了。$g = "/^1[34578]\d{9}$/"上面是正则表达式,那怎么用PHP来写呢?$g = "/^1[34578]\d{9}$/" if(preg_match(...

Thinkphp6 把用Db类的条件查询转成原生的sql语句

Thinkphp6经常需要查看 SQL 原生语句,这里有两种方式获取:1、getLastSql(), 获取方法前最后一条 SQL 原生语句$a1 = Movies::where('state',1)->limit(10)->select(); $a2...