Title: How to make ZF2 db credentials environment specific
Tags: php;git;configuration;zend-framework2;environment-variables
Question: I am starting a new project.
I am using ZF2. I have just installed it and have the Skeleton application up and running.
This is my deployment process:
I develop on my local machine
I then push to my public github repository
I then use deployhq.com to deploy those to my production
server which is where the user would see the changes made.
I have tried to look around stack, zend site, and google at blogs etc but still dont have any real understanding or solution to my problem.
I want the application to use different database credentials based on its environment. E.g. if on 'dev', my local machine, then use credentials A, but if on live server, then use credentials B.
I have read a lot about global and local autoload config files etc, but baring in mind my github repo is public, any where I commit any config files with my db details would be visible.
I was wondering if there was a way to have, the same theory, global and local files with the DB connections in, i upload the production details manually, not via git for security reason, and tell git to ignore the local config file somehow? I would also need to know how I tell the application to use those config files based on the environment and there location.
Comment: Place sensitive config options to gitignored `config/autoload/*.local.php` files. If you want to provide placeholder config, put it to `config/autoload/*.local.dist`. Environment specific options you can commit as `*.local.dist-dev`, `*.local.dist-production`, etc and copy appropriate files on deployment

Here is the accepted answer: In Zend 2 There are
Global configuration file &amp;
Module level configuration file
IF you want to know there use you can refer the link below
How does configuration works in ZF2
When I had a same scenario I   used the above link to understand and exploit Zend Config module which is really good to handle the situation like this  .
create two files
production.php
local.php
in both these files
return this array based on the environment
return array(
"dbname" =&gt; "yourdbname"
"dbhostname" =&gt; "dbhostname"
"dbusername" =&gt; "yourdbusername",
"dbpassword" =&gt; "yourdbpassword"
in config/autoload/ directory of your zend framewrok application
later edit your config/application.config.php file as per below instructions
// get the application env from Apache vhost file ( here you can set in your apache vhost file as production or local )
$applicationEnv = getenv('APPLICATION_ENV');
$environmentSpecificConfigPath = "config/autoload/{,*.}{".$applicationEnv.",local}.php";
// Next with in the config array pass the environment specific configuration path
'config_glob_paths' =&gt; array($environmentSpecificConfigPath)
in any controller or action
you can just use the below code
$configArray = $this-&gt;getGlobalConfig();
Now $configarray has all your DB credentials to create a connection
$adapter = new Zend\Db\Adapter\Adapter(array(
'driver' =&gt; 'Mysqli',
'database' =&gt; $configArray['dbname'],
'username' =&gt; $configArray['dbusername'],
'password' =&gt; $configArray['dbpassword']
If you use config array to connect the DB in your entire application
you dont need to worry about environment changes just make sure you have an Apache APPLICATION_ENV entry in your vhost file
you can do that by adding below line in your apache vhost file
SetEnv APPLICATION_ENV "production" // in your production server
SetEnv APPLICATION_ENV "local"  // in your local
Also Last but not least you can use the Zend Experts module ZeDB
https://github.com/ZendExperts/ZeDb
To manage your CRUD applications
Hope the above steps may help you in creating the environment
Comment for this answer: @aravind.udayashankara thanks, this instruction has a great and simple approach.
Comment for this answer: Nice instructions! However the first link 'How does configuration works in ZF2' is broken. Cheers
Comment for this answer: @PetterSoderlund , Thanks for notifying me, Sorry for not updating this for a long time, Please check I have corrected the URL, the content of the old URL has been moved to ZF2 documentation, Before it was incomplete.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:09/20/13', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<php><git><configuration><zend-framework2><environment-variables>', 'Title': 'How to make ZF2 db credentials environment specific', 'ViewCount': 2449, 'id': 18916531, 'user_AboutMe': None, 'user_AccountId': 162315.0, 'user_DisplayName': 'RSM', 'user_Reputation': 13870.0, 'user_Views': 1138.0, 'user_id': 383691.0}}