How to create a command (task) in Symfony 2

This tutorial is deprecated. It was written when Symfony2 was in beta. 

In Symfony 1.x a task is a command line tool to help perform tasks around the application. The same is in Symfony 2. Unfortunately in Symfony2 there is no tool to auto-generate some code for these. To create a Symfony2 Command you must to have or to create in your Bundle a folder named Command. Here, you must create a file named MytaskCommand.php with the following code:

 

<?php
namespace ApplicationVremeaBundleCommand;

use SymfonyBundleFrameworkBundleCommandCommand;
use SymfonyComponentConsoleInputInputDefinition;
use SymfonyComponentConsoleInputInputOption;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use SymfonyComponentRazvanUtilsCurl;

class ImportCommand extends Command {

 const NEWLINE = true;
 private $connection;

 protected function configure() {
 parent::configure();

 $this
                ->setName('tudorica:razvan')
                ->addArgument('type', InputArgument::REQUIRED, 'Type')
                ->addOption('country', 'c', InputOption::VALUE_OPTIONAL, 'Country', '')

        ;
    }

    /**
     * Executes the current command.
     *
     * @param InputInterface  $input  An InputInterface instance
     * @param OutputInterface $output An OutputInterface instance
     *
     * @return integer 0 if everything went fine, or an error code
     *
     * @throws LogicException When this abstract class is not implemented
     */
    protected function execute(InputInterface $input, OutputInterface $output) {
        // your code here.
        //if you want to access database (must to setup doctrine.dbal)
        $this->connection = $this->container->get('database_connection');

         //to get the user input argument "type" for this command
        $type = $input->getArgument('type');        

        //to get use option "contry" for this command
        $country = $input->getOption('country');

        //to write a message (in symfony1.x was named log).
        $output->write('type: '.$type, true);
    }

}

N

ow, you can open a console and go to app folder. If you write:

./console

you will see something like

router
  :debug                       Displays current routes for an application
  :dump-apache                 Dumps all routes as Apache rewrite rules
tudorica
  :razvan

You can execute this Command with: ./console tudorica:razvan

 

3 thoughts on “How to create a command (task) in Symfony 2”

  1. this “how to” is from the time when symfony2 was in beta, so it is possible to not work anymore. please check documentation.

  2. Just copy an existing task from a vendor bundle (e.g. Doctrine or swiftmailer) of the Symfony2 stable release to your own bundle’s Command directory and you have a nice and up-to-date template for your own command.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.