ホームページ  >  記事  >  PHPフレームワーク  >  ThinkPHP でコマンドライン (cli) think コールを使用する方法

ThinkPHP でコマンドライン (cli) think コールを使用する方法

coldplay.xixi
coldplay.xixi転載
2020-06-30 17:47:244625ブラウズ

ThinkPHP でコマンドライン (cli) think コールを使用する方法

一部のシナリオでは、コマンド ラインからコードを呼び出す必要があります。

  1. まず、application\command でコードを呼び出す必要があります。ディレクトリに新しい hello.php を作成します (存在しない場合は作成します):
    <?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. application/command.php を変更します (存在しない場合は作成します)
    <?php
    return [
     "app\command\hello",];
  3. cd toプロジェクトのルート ディレクトリに移動し、コマンド ラインで
    php think hello
    # と入力します。
  4. ##OK、
    hello world
    の呼び出しに成功しました
#関連学習の推奨事項:
PHP プログラミングの入門から熟練度まで

以上がThinkPHP でコマンドライン (cli) think コールを使用する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はlearnku.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。