Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the environment in a Symfony2 console command

Tags:

symfony

Hopefully a simple question - how does one specify which environment to use when running a console command in Symfony2. I've created a few commands, however I would like to run them in the context of my 'staging' environment when on my staging server and my 'prod' environment when on my production server (the different environments define different database connections). How do I configure and pass this information to my console command?

like image 246
Marc Avatar asked Aug 18 '11 22:08

Marc


People also ask

Where is .ENV file Symfony?

Selecting the Active Environment Symfony applications come with a file called . env located at the project root directory.

What is bin console in Symfony?

The Symfony framework provides lots of commands through the bin/console script (e.g. the well-known bin/console cache:clear command). These commands are created with the Console component. You can also use it to create your own commands.

What is Symfony Console component?

The Console component eases the creation of beautiful and testable command line interfaces. The Console component allows you to create command-line commands. Your console commands can be used for any recurring task, such as cronjobs, imports, or other batch jobs.

How do I know what version of Symfony I have?

If you have file system access to the project Look inside the file for a line like: const VERSION = '5.0. 4'; that's the Symfony version number.


1 Answers

You have two options that will help you out. You can specify the environment that the console will run in with the --env flag, and use --no-debug to disable debug mode.

php app/console --env=staging your:console:command or php app/console --env=prod your:console:command should do what you're looking for (the console runs in the dev environment with debug on by default).

You can look at the code of the app/console file for more info.

like image 70
Derek Stobbe Avatar answered Sep 22 '22 08:09

Derek Stobbe