Symfony2 Console Commands CheatsheetJune 19, 2014

How to use console commands with Symfony2

To run any Symfony2 console command, open up a command prompt and navigate to your app’s directory, then run the console with a command:

C:\> cd htdocs/myappdirectory C:\htdocs\myappdirectory> php app/console list

‘List’ will give you a list of the commands.

Common Symfony2 console commands I’m somehow always using but always forgetting

To see a list of routes (and more importantly which ones are missing):

C:\me> php app/console router:debug
To update a database automatically after you've made some changes to an entity:
C:\me> php app/console doctrine:schema:update --force

To generate CRUD operations in a controller for an entity you’ve created

C:\me> php app/console generate:doctrine:crud
To clear the cache or warm it up:
C:\me> php app/console cache:clear C:\me> php app/console cache:warmup

Update the app’s public directory with current assets:

C:\me> php app/console assets:install

To generate a basic bundle or controller:

C:\me> php app/console generate:bundle C:\me> php app/console generate:controller

then follow the prompts.

To locate a pesky YAML error:

C:\me> php app/console yaml:lint src\Acme\DemoBundle\Resources\config\routing.yml

To display all possible commands:

C:\me> php app/console list

Make your own Symfony2 console command

Imagine that you work on a lot of projects that are very similar… They have the same User entities, the same Role entities… The same bundle structure, CRUD operations in the controller, etc. You could so very easily automate the process of creating such generic apps by creating your own console commands.

In this example I’ll create a trivial command that takes a question as an argument and gives you a magic 8-ball response (because it’s slightly more fun for me than writing a command that automates the process of creating a generic app):

You just need to create a Command that extends Command, and name it CommandNameCommand.php and place it in the Bundle’s Command directory:

namespace Acme\Bundle\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class Magic8BallCommand extends Command { protected function configure() { $this ->setName('magic:8ball') ->setDescription('Magic 8 Ball predicts the future') ->addArgument('question', InputArgument::OPTIONAL,'What question do you ask the Magic 8 Ball?'); } protected function execute(InputInterface $input, OutputInterface $output) { $answers = array('yes', 'no', 'maybe', 'it is a secret'); $question = $input->getArgument('question'); if ($question) { $response = $answers[rand(0, 3)]; } else { $response = 'Hello'; } $output->writeln($response); } }

Run the command, ask it a question (put the question inside quotes or you’ll get a too many arguments exception):

C:\me> php app/console magic:8ball "Will the people who go off to colonise Mars live long lives?"

I won’t tell you what the answer was…

Category: Cheat Sheets

Leave a Reply

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