Home >PHP Framework >ThinkPHP >How to use the command line (cli) think call in ThinkPHP

How to use the command line (cli) think call in ThinkPHP

coldplay.xixi
coldplay.xixiforward
2020-06-30 17:47:244732browse

How to use the command line (cli) think call in ThinkPHP

In some scenarios, we need to call the code in the command line

  1. First, in application\command Create a new hello.php in the directory (create it if it does not exist):
    <?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;
    use think\Request;
    class hello extends Command {
     /**
      * 重写configure
      * {@inheritdoc}
      */
     protected function configure()
     {
         $this
             // 命令的名字("think" 后面的部分)
             ->setName(&#39;hello&#39;)
             // 配置一个参数 使用$input->getArgument(&#39;username&#39;)获取
             // ->addArgument(&#39;username&#39;)
             // 运行 "php think list" 时的简短描述
             ->setDescription(&#39;定时任务微服务.&#39;)
             // 运行命令时使用 "--help" 选项时的完整命令描述
             ->setHelp("定时任务微服务 无参数");
     }
     /**
      *  * 重写execute
      *  * {@inheritdoc}
      *  
      * @param Input $input
      * @param Output $output
      */
     protected function execute(Input $input, Output $output)
     {
         echo &#39;hello world&#39;;
     }}
  2. Modify application/command.php (create it if it does not exist)
    <?php
    return [
     "app\command\hello",];
  3. cd to the project root directory and enter
    php think hello
    # on the command line.
  4. ##OK, successfully called
    hello world
Related learning recommendations:

PHP programming from entry to proficiency

The above is the detailed content of How to use the command line (cli) think call in ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete