Using Symfony2 console the right way
Posted on: 2014-12-07 | Categories:
In my daily work, I really often use the Symfony2 console. I used to type scommand
$ php app/console each time I wanted to execute a script. Sometimes I made a typo, sometimes I just forgot to add proper
env parameter – the thing is that I lost some time. In this short entry I want show you how to speed up the work with the console and improve your performance.
Note: the following tips will also work for any other commands – not only for PHP or Symfony2 framework commands.
Develop faster… with aliases
It is no secret that the longer command we type, the easier to make a mistake. What can we do? The solution is simple: we should create aliases. At the beginning, a very simple example:
$ alias sf="php app/console"
I assume that we are in the Symfony2 app directory.
From now on, we can type in the console commands as follows :
$ sf list
$ sf cache:clear
$ sf generate:bundle
We can even go one step further and create separate aliases for each environment:
$ alias sfdev="php app/console --env=dev"
$ alias sfprod="php app/console --env=prod"
To develop even faster we can create dedicated aliases for common used commands like:
$ alias sf:cc="php app/console cache:clear"
# and others
$ alias sf:gb="php app/console generate:bundle"
$ alias sf:rd="php app/console router:debug"
$ alias sf:dsd="php app/console doctrine:schema:update"
$ alias sf:dmd="php app/console doctrine:migrations:diff"
$ alias sf:dmx="php app/console doctrine:migrations:execute"
It all depends on what will be convenient for us. But remember, short and not very intuitive aliases may do more harm than good.
Tip: You can also display frequently used Symfony2 commands (or any other commands) typing:
history | grep 'php app/console' in console. This will return list of recently typed commands.
Pro tip: If you remember only part of the command, you can try a reverse intelligent search pressing:
CTRL + R and typing just a few letters. The search will return first command from history in reverse order that matches the letters we have typed.
How to create permanent bash alias?
Each created alias will be restored after reboot unless we save it in proper file.
To create permanent alias we can edit
… and add aliases at the bottom:
alias sf="php app/console"
This need reloading:
Notice: It’s not recommended to add aliases directly to
.bashrc file. We should create separate file e.g.
bash_aliases and place aliases there. We would also need to include our new created file in
.bashrc file with following line:
. ~/.bash_aliases .