9951 explained code solutions for 126 technologies


php-symfony-consoleHow to create a Symfony console application in PHP?


Creating a Symfony console application in PHP is a simple process.

First, create a new project using the Symfony CLI:

$ symfony new my_project

This will create a new project in the my_project directory.

Next, create a new command class in the src/Command directory:

$ touch src/Command/MyCommand.php

The command class should extend the Symfony\Component\Console\Command\Command class and implement the configure() and execute() methods.

Finally, register the command in the config/services.yaml file:

services:
    App\Command\MyCommand:
        tags:
            - { name: console.command }

The command can now be executed using the Symfony CLI:

$ php bin/console my:command

Helpful links

Edit this code on GitHub