6自定义指令
未知
2021-07-04 11:52:27
0

创建自定义指令

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

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);
    }
}
复制

这个文件定义了一个叫hello的命令,并设置了一个name参数和一个city选项。

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

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

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

php think
复制

输出

Think Console version 0.1

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -V, --version         Display this console version
  -q, --quiet           Do not output any message
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  build              Build Application Dirs
  clear              Clear runtime file
  hello              Say Hello 
  help               Displays help for a command
  list               Lists commands
 make
  make:controller    Create a new resource controller class
  make:model         Create a new model class
 optimize
  optimize:autoload  Optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production.
  optimize:config    Build config and common file cache.
  optimize:schema    Build database schema cache.

复制

第四步,运行hello命令

php think hello
复制

输出

Hello thinkphp!
复制

添加命令参数

php think hello kancloud
复制

输出

Hello kancloud!
复制

添加city选项

php think hello kancloud --city shanghai
复制

输出

Hello kancloud!
From shanghai
复制

注意看参数和选项的调用区别

如果需要生成一个指定的命名空间,可以使用:

php think make:command app\index\Command second
复制

在控制器中调用命令

支持在控制器的操作方法中直接调用命令,例如:

<?php
namespace app\index\controller;

use think\facade\Console;

class Index
{
    public function hello($name)
    {
        $output = Console::call('hello', [$name]);

        return $output->fetch();
    }
}
复制

访问该操作方法后,例如:

http://serverName/index/hello/name/thinkphp
复制

页面会输出

Hello thinkphp!
复制

相关内容

《哪吒2》中国影史第一个8...
《哪吒2》中国影史首个80亿大作,以卓越艺术与技术成就铸就辉煌,再...
2025-02-10 07:00:43
如何选择和创建MySQL索...
选择MySQL索引需考虑业务需求、查询类型等因素,适当使用单列或多...
2025-02-09 09:23:40
PHP中pcntl_str...
PHP中,pcntl_strerror()函数用于将系统错误号转换...
2025-02-09 02:00:49
创建MySQL索引的步骤
摘要: 本文介绍了在MySQL数据库中创建索引的步骤,包括确定需...
2025-02-06 23:23:52
MySQL索引基础
MySQL索引是数据库表中的特殊数据结构,可快速定位数据,提高查询...
2025-02-04 23:23:45
创建索引的最佳实践
摘要: 创建高效索引是数据库管理的关键,遵循最佳实践包括理解业务...
2025-02-04 22:23:43

热门资讯

tp6开发规范 命名规范 请理解并尽量遵循以下命名规范,可以减少在开发过程中出现不必要的错误。 ThinkPHP6....
6高级查询 快捷查询 快捷查询方式是 一种多字段相同查询条件 的简化写法,可以进一步简化查询条件的写法,在多个字...
tp6多应用提示控制器不存在:... 第一个情况是没有使用composer安装扩展。 如果要使用多应用模式, 你需要win+r,cmd指针...
6配置 配置目录 单应用模式 对于单应用模式来说,配置文件和目录很简单,根目录下的 config 目录下面就...
6异常处理 和PHP默认的异常处理不同,ThinkPHP抛出的不是单纯的错误信息,而是一个人性化的错误页面。 异...
6swoole 本篇内容主要讲述了最新的 think-swoole 扩展的使用。目前仅支持Linux环境或者MacO...
6助手函数 助手函数 系统为一些常用的操作方法封装了助手函数,便于使用,包含如下: 助手函数 描述 abort ...
6查询表达式 查询表达式 查询表达式支持大部分的SQL查询语法,也是 ThinkPHP 查询语言的精髓,查询表达式...
6路由参数 路由参数 路由分组及规则定义支持指定路由参数,这些参数主要完成路由匹配检测以及后续行为。 路由参数可...
6查询 模型查询和数据库查询方法的区别主要在于,模型中的查询的数据在获取的时候会经过获取器的处理,以及更加对...