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

$ app/console list

you can omit the list and just run

$ app/console

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:

$ app/console list doctrine

You can be more specific again with:

$ app/console list doctrine:mapping

to just see the Doctrine mapping related commands.

Help

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

$ app/console help

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

$ app/console help router:debug

Shell

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:

$ app/console --shell
Symfony > cache:clear 

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:

$ app/console ca:c

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:

$ app/console clear:cache --env=prod

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:

$ app/console foq:elastica:populate --no-debug