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

thinkphp6 创建自定义命令行指令

zhangsir3年前 (2022-11-04)php244

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

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

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

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

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

标签: phpswoole
分享给朋友:

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

迅睿CMS如何在列表循环中调用模块附表内容字段

列表循环标签改一下:join=1_news_data_0 on=id例如列表循环时,加上的效果{module catid=$catid join=1_news_data_0 on=id order=updatetime page=1}这个写法仅限于5万以内的数据...

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

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

php 爬虫函数

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

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

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

thinkphp 使用Filesystem类提示未找到Filesystem类

这是因为thinkphp官方升级了thinkphp版本的原因,只需要更新Filesystem类就行了更新命令行如下:composer require topthink/think-filesystem 1.0.1...

PHP获取当前请求的所有请求头信息

apache_request_headers()函数里面保函了所有的请求头信息//获取请求头 $headers = apache_request_headers(); var_dump($header)这样就打印出所有的请求头信息了。...