Symfony2: Console Usage Notes

There is quite a lot of documentation, blog posts etc out there on writing console commands for Symfony2. Here though I am going to look at the more basic usage of the console with a few notes on getting more out of it.

Listing Commands

You can get a list of the options for running commands as well as the currently registered commands by runnning

you can omit the list and just run

for the same output.

Third party bundles can register new commands so the list can change as you install more bundles into an application.

The commands are namespaced using : as the separator, you can list just the commands in a particular namespace by specifying the namespace with the list commands. For example to just see a list of doctrine commands:

You can be more specific again with:

to just see the Doctrine mapping related commands.


You can just get usage details and options without the list of commands with

You can get help specific to an individual by specifying that command a well, e.g.:


If you have several commands to run then using the shell option will stop you being returned to the normal command line and allow you to just run Symfony2 commands without the need for specifying app/console:

Shortcut Syntax

You can avoid having to type out the full names of commands by just typing the shortest part needed to make it unambiguous. For example, to clear the cache you can just type:

ca rather than c is needed to avoid ambiguity with config and container.

Different Environments

By default when you run a console command it will run in the dev environment. You can change this by specifying the environment:

For some commands the environment you run in important because it will change the effect of running the command e.g. cache:clear and assetic:dump. For other commands though they will run the same but you will get them to run quicker and use less memory by using the prod environment – remember to clear the production cache if necessary first though.

You can still run commands in the dev environment and get some performance gain for commands which collect a lot of debug information by turning off debug mode. For example, the populate command in the FOQElasticaBundle will, by default, collect a lot of debug information which will cause it to use a lot of memory when indexing a lot of entities, this can be avoided by specifying no debug:

Symfony2: Coding Standards Update

I posted about coding standards in Symfony2 and how to check them a while ago. Since then the standard used has changed as Symfony2 has adopted the PSR 0,1,2 standards. This is just a very quick post to update my earlier post since some of the information in it is now out of date.

Checking these standards will be one of the standards bundled with PHPCodeSniffer although work is still continuing on this at the moment. You can get the work in progress if you get PHPCodeSniffer from github instead of installing it using Pear. Once this is complete there will be no need to install a custom standard in order to check that your Symfony2 project code meets these standards, you can just run /path/to/phpcs --standard=PSR2. Using PSR2 as the standard will automatically check PSR1 and 0 as well as they are subsets of PSR2.

As well as just checking the standards, you can now also use Fabien Potencier’s coding standards fixer, which not just checks but fixes code to meet these standards where possible making fixing it a much less tedious process.