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

thinkphp6 创建自定义命令行指令

zhangsir3年前 (2022-11-04)php221

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

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://www.mianka.xyz/post/58.html

标签: phpswoole
分享给朋友:

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

php 数组转json,json转数组

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

PHP下载中文名称的文件,文件名乱码的解决方法!

PHP中,如果要下载的文件名称为中文,则会出现文件标题乱码。此时就需要对标题进行编码,也就是说先进性urlencode,然后再放入header,然后问题就解决了。$filename = urlencode("下载文档"); header (&nbs...

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

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

think PHP返回上一页的办法!

think PHP返回上一页的办法!输入如下代码即可返回上一页return redirect($_SERVER["HTTP_REFERER"]);...

thinkphp 利用PHPMailer三方类发送邮件

1.首先用composer下载PHPMailer,在网站根目录进入命令行输入如下命令即可composer require phpmailer/phpmailer2.然后创建文件Ma.php,填写如下代码<?php namespace app\controller...

php 判断当前请求是http请求还是https请求!

php判断http请求还是https请求$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'...