Revision: Thu, 30 Mar 2023 18:23:12 GMT

Getting started — First CLI command

Spiral provides convenient way to create console applications. It has the built-in support for console commands, which allows you to create command-line interfaces (CLIs) for your application. With console commands, you can automate tasks, perform maintenance, and interact with your application in a way that is not possible with a standard web interface.

Managing and using console commands in Spiral is very easy. The framework provides a convenient interface for working with console commands by leveraging the power of the symfony/console package.

Here are the basic steps to creating a console command in Spiral:

Creating a command

Here's an example of a basic console command that outputs current date to the console:

namespace App\Endpoint\Console;

use Spiral\Console\Command;

final class CurrentDateCommand extends Command
    protected const SIGNATURE = 'current:date {format=Y-m-d : Date format}';
    protected const DESCRIPTION = 'Get current date';

    public function __invoke(): void

Spiral is configured by default to automatically discover commands located in the app/src directory using the static analysis component. This means that you don't have to manually register your commands or create a separate configuration file for them.

Running the command

To get help information for your command, you can run the followed command in your terminal.

php app.php help current:date

This will display the command's signature, description, and any available arguments or options.

Get current date
current:date [<format>]
format Date format [default: "Y-m-d"]
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) 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

That's it! You've successfully set up your first console command in Spiral.

What's Next?

Now, dive deeper into the fundamentals by reading some articles:

Edit this page