Using Symfony2 console the right way
Posted on: 2014-12-07 | Categories:
PHP
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 .bashrc
file:
… 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
.
enekochan
December 7, 2014 15:54
The aliases with spaces around the equal sign don’t work in Mac OS X. For example:
Nice post by the way! I’ve just configured my aliases this way 🙂 Thanks!
Kamil
December 8, 2014 20:08
Thanks, i just fixed it.
Florian Klein
December 7, 2014 21:33
Thanks for sharing!
Are you aware that symfony already allows shortcuts for commands ?
Any command can be type with the minimum amount of letters to remove any ambiguity.
For example:
No need to hardcode those as a shell alias.
Next: app/console can be made executable (it is if correctly installed).
The result is that you can avoid typing php all the time 🙂
Next: app/console is just a symfony-standard thing.
No one forbids you to move this app/console file at the root and name it sf.
Just modify the “../..” paths to take care of its new position.
All in all, you end up with just:
Isn’t that beautiful ? 🙂
No shell alias anymore.
PS: I also use a shell alias for app/console, but I wanted to share my thoughts 🙂
Cheers.
Kamil
December 8, 2014 20:31
I didn’t know that, thanks for pointing out!
I agree that commands like:
are really nice but they are not perfect. Using default Symfony2 shortcuts there are many ambiguous commands like:
So aliases in this case are really useful 😉
Cheers.
Piero Recchia
December 14, 2014 16:33
Great post, by the way Symfony have a shell too, with autocomplete only type php app/console –shell and done.
Cheers.
Kamil
December 30, 2014 11:33
Yes, Symfony2 shell in nice but i think that using aliases is more efficient and faster.
Cheers.