自定义命令行-j9九游

创建自定义命令行

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


return [
    'app\home\command\test',
];

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


namespace app\home\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:");
    }
}

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

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

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
  help               displays help for a command
  list               lists commands
  test               here is the remark 
 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:route     build route cache.
  optimize:schema    build database schema cache.

第四步,运行test命令

php think test

输出

testcommand:
文档最后更新时间:2018-04-26 11:03:57
网站地图