thinkphp6自定义命令行命令不生效-[InvalidArgumentException] Command "test" is not defined.

2021-03-28 19:12 阅读:5692

按照官方的写法不生效,做个笔记。

  • 将命令放在app\AppService.php文件中绑定(没有这个文件则创建)
<?php
declare (strict_types = 1);

namespace app;

use think\Service;

/**
 * 应用服务类
 */
class AppService extends Service
{
    public function register()
    {
        // 服务注册
    }

    public function boot()
    {
        // 服务启动
        $this->commands([
            'test' => \app\index\command\Test::class
        ]);
    }
}
  • 对应的Test.php文件
<?php


namespace app\index\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

class Test extends Command
{
    protected function configure()
    {
        $this->setName('test')->setDescription('Here is the remark ');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln("TestCommand:");
    }
}
  • 然后执行php think 
W:\www\regaing>php think
version 6.0.6

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 App Dirs
  clear             Clear runtime file
  help              Displays help for a command
  list              Lists commands
  run               PHP Built-in Server for ThinkPHP
  test              Here is the remark                     //这个就是刚才绑定的命令名称
  version           show thinkphp framework version
  • 然后执行php think test 
W:\www\regaing>php think test
TestCommand:

W:\www\regaing>


{{commentTotal}} 条评论

{{item.nickname}}
{{item.create_date}}
{{item.content}}
- 上拉或点击加载更多 -
- 加载中 -
- 没有更多了 -
- 本文链接 -