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

thinkphp6 创建自定义命令行指令

zhangsir3年前 (2022-11-04)php269

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

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

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

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

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

标签: phpswoole
分享给朋友:

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

php获取数组的长度的方法

1、count、sizeof 都可以直接统计一维数组长度。2、例如:$arr = Array('0','1','2','3','4'); echo count($arr);// 输...

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

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

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

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

thinkphp6使用swoole步骤实例

一,给PHP软件下载swoole插件二,使用thinkphp6的自定义指令功能php think make:command Tcp tcp三,修改Tcp.php文件(位于:app\command\Tcp)<?php namespace app...

用PHP写个递归函数

以下是一个使用 PHP 编写的递归函数示例,该函数将计算给定数字的阶乘:function factorial($n) {     if ($n <= 1) {   &nb...