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

thinkphp6 创建自定义命令行指令

zhangsir3年前 (2022-11-04)php343

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

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版权y1防采集https://mianka.xyz

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

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

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

标签: phpswoole
分享给朋友:

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

php7.4 安装zip扩展包

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

php 数组转json,json转数组

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

php 爬虫函数

 public function request_post($url = '', $param = '')     {   ...

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

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

think PHP返回上一页的办法!

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