Book Image

Symfony2 Essentials

Book Image

Symfony2 Essentials

Overview of this book

Table of Contents (17 chapters)
Symfony2 Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating the console command


To create the console command, we need to create a special class with the Command suffix. This suffix is required so that Symfony2 can find and automatically attach your class to the Symfony2 application console.

See the following example of the basic command:

<?php

// File src/AppBundle/Command/HelloCommand.php
namespace AppBundle\Command;

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

class HelloCommand extends Command
{
    protected function configure()
    {
        $this
            ->setName('todo:hello')
            ->setDescription('this is simple, hello command')
        ;
    }

    protected function execute(
        InputInterface $input, 
        OutputInterface $output
    )
    {
        $output->writeln('Hello world!');
    }
}

When you add this file and execute the app/console command, you would see that it has been attached...