Using Symfony2 console the right way

Posted on: 2014-12-07 | Categories: PHP

Symfony2 console 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:

I assume that we are in the Symfony2 app directory.
From now on, we can type in the console commands as follows :

We can even go one step further and create separate aliases for each environment:

To develop even faster we can create dedicated aliases for common used commands like:

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 .bashrc file:

… and add aliases at the bottom:

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 .