创建自定义命令行

第一步,配置command.php文件,目录在application/command.php

  1. <?php
  2. return [
  3. 'app\home\command\Test',
  4. ];

第二步,建立命令类文件,新建application/home/command/Test.php

  1. <?php
  2. namespace app\home\command;
  3. use think\console\Command;
  4. use think\console\Input;
  5. use think\console\Output;
  6. class Test extends Command
  7. {
  8. protected function configure()
  9. {
  10. $this->setName('test')->setDescription('Here is the remark ');
  11. }
  12. protected function execute(Input $input, Output $output)
  13. {
  14. $output->writeln("TestCommand:");
  15. }
  16. }

这个文件定义了一个叫test的命令,备注为Here is the remark,执行命令会输出TestCommand。

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

  1. php think

输出

  1. Think Console version 0.1
  2. Usage:
  3. command [options] [arguments]
  4. Options:
  5. -h, --help Display this help message
  6. -V, --version Display this console version
  7. -q, --quiet Do not output any message
  8. --ansi Force ANSI output
  9. --no-ansi Disable ANSI output
  10. -n, --no-interaction Do not ask any interactive question
  11. -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
  12. Available commands:
  13. build Build Application Dirs
  14. clear Clear runtime file
  15. help Displays help for a command
  16. list Lists commands
  17. test Here is the remark
  18. make
  19. make:controller Create a new resource controller class
  20. make:model Create a new model class
  21. optimize
  22. optimize:autoload Optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production.
  23. optimize:config Build config and common file cache.
  24. optimize:route Build route cache.
  25. optimize:schema Build database schema cache.

第四步,运行test命令

  1. php think test

输出

  1. TestCommand: