Creating a Custom ThinkPHP Command Line Script (hello)

This tutorial explains how to create a custom ThinkPHP command line script by adding a hello.php class in the application/command directory, configuring its name, description, and help, registering it in application/command.php, and executing it with the 'php think hello' command to output 'hello world'.

php Courses
php Courses
php Courses
Creating a Custom ThinkPHP Command Line Script (hello)

This guide shows how to add a custom command to a ThinkPHP project that can be run from the command line.

First, create

<?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
     */
    protected function configure()
    {
        $this
            ->setName('hello')
            // ->addArgument('username')
            ->setDescription('定时任务微服务.')
            ->setHelp("定时任务微服务 无参数");
    }

    /**
     * 重写execute
     *
     * @param Input $input
     * @param Output $output
     */
    protected function execute(Input $input, Output $output)
    {
        echo 'hello world';
    }
}

in the application/command directory (create the directory if it does not exist). This class extends Command and defines the command name, description, help text, and the logic that prints hello world.

Next, ensure the command is registered by creating or editing application/command.php with the following content:

<?php
return [
    "app\\command\\hello",
];

Finally, from the project root, run the command using the ThinkPHP console: php think hello The command executes and outputs: hello world This confirms that the custom ThinkPHP command line script is set up correctly.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPcommand-lineThinkPHP
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.