Reading list Switch to dark mode

    How to create console command in Symfony

    Updated 9 January 2022

    Today we are going to learn to create symfony commands. You must have already used many symfony console commands. i.e. bin/console list doctrine or bin/console clear:cache

    Sometimes we need to create our own commands and need to execute our custom code through the symfony command.

    So lets start learning symfony commands. we will do it step by step.

    Create class for the Command

    We have to create the class for the command under src/Command/ directory.
    Lets create our class for custom command src/Command/CustomCommand.php

    Start your headless eCommerce
    now.
    Find out More

    #Code for class of command
    // src/Command/CustomCommand.php
    
    <?php
    
    namespace App\Command;
    
    use Symfony\Component\Console\Command\Command;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Output\OutputInterface;
    use Symfony\Component\Console\Input\InputArgument;
    
    class CreateUserCommand extends Command
    {
        // In this function set the name, description and help hint for the command
        protected function configure(): void
        {
            // Use in-build functions to set name, description and help
    
            $this->setName('my-custom-command')
                ->setDescription('This command runs your custom task')
                ->setHelp('Run this command to execute your custom tasks in the execute function.')
                ->addArgument('param', InputArgument::REQUIRED, 'Pass the parameter.');
        }
    
        // write the code you want to execute when command runs
        protected function execute(InputInterface $input, OutputInterface $output): int
        {
            // If you want to write some output
            $output->writeln('Pass the parameter' . $input->getArgument('param'));
    
            // Return below values according to the occurred situation
    
            if (SUCCESSFUL_EXECUTION_CONDITION) {
    
                // if everything is executed successfully with no issues then return SUCCESS as below
                return Command::SUCCESS;
    
            } elseif (EXECUTION_FAILURE_CONDITION) {
    
                // if execution fails return FAILURE as below
                return Command::FAILURE;
    
            } elseif (INVALID_EXECUTION_CONDITION) {
    
                // if invalid things happens i.e. invalid arguments etc. then return INVALID as below
                return Command::INVALID
    
            }
        }
    }

    Lets understand the components in the command class –

    Included namespaces

    use Symfony\Component\Console\Command\Command;
    Every custom command class must extend the Command class for working. So use Command class namespace in commad php file.

    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Output\OutputInterface;

    Above two classes are used for input and output operations while executing the custom commands. See the execute() function how these classes and used in the class parameters and used inside the function for input/output operations.

    use Symfony\Component\Console\Input\InputArgument;
    Include this class when you have to pass any argument while executing the command. See the configure() function how ‘param’ is set as a required argument.

    configure() function

    This function is used to set configurations for the console command.
    In this function you can set the configurations like name, description and help hint for the console command.

    // In this function set the name, description and help hint for the command
    protected function configure(): void
    {
        // Use in-build functions to set name, description and help
    
        $this->setName('my-custom-command')
            ->setDescription('This command runs your custom task')
            ->setHelp('Run this command to execute your custom tasks in the execute function.')
            ->addArgument('param', InputArgument::REQUIRED, 'Pass the parameter.');
    }

    execute() function

    In this function, write the code to be executed on running the created console command.

    You can return the constants according to the code execution conditions :
    Command::SUCCESS : if successful execution
    Command::FAILURE : if execution fails
    Command::INVALID : if invalid execution (i.e. invalid arguments etc.)

    // write the code you want to execute when command runs
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        // If you want to write some output
        $output->writeln('Pass the parameter' . $input->getArgument('param'));
    
        // Return below values according to the occurred situation
    
        if (SUCCESSFUL_EXECUTION_CONDITION) {
    
            // if everything is executed successfully with no issues then return SUCCESS as below
            return Command::SUCCESS;
    
        } elseif (EXECUTION_FAILURE_CONDITION) {
    
            // if execution fails return FAILURE as below
            return Command::FAILURE;
    
        } elseif (INVALID_EXECUTION_CONDITION) {
    
            // if invalid things happens i.e. invalid arguments etc. then return INVALID as below
            return Command::INVALID
    
        }
    }

    In Symfony, Console commands are registered as services. All commands have console.command tag.
    in the default services.yml configuration file commands are registered automatically by autoconfigurations.

    Command Execution

    After configuring and registering the command, you can run it in the terminal:

    php bin/console my-custom-command

    As soon as you run your command on console, the code written in the execute() function will be executed.

    So this is how you can create and execute the console command in symfony.

    You can also use these console commands to run cron jobs in symfony.

    Below is an example to run a cron job using the symfony command :

    * * * * root /usr/bin/php /path/to/bin/console command:to:execute

    Hope this blog will help you in future development in symfony.

    reference : symfony console commands

    Happy coding. 🙂

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home