Welcome Guest, Not a member yet? Register   Sign In
Retrieving configs from a file other than config.php
#1

[eluser]merik[/eluser]
I am working with a few friends on a project using CI. The code is kept on a git repository, and each of us has a copy of it locally. The configurations we each need to run an instance of the code locally are different; for example, the DB name, the DB password, etc.

If one of us changes those configs in config.php and pushes it back to the git repository, all others will be affected on the next pull. Therefore, we are thinking if there is a way to keep all the configurations in a file ignored by git (using .git-ignore) which is located in the root folder and then make config.php load them from that file.

Is this supported in CI? If not, what is the easiest way to implement it?
#2

[eluser]TheFuzzy0ne[/eluser]
CodeIgniter supports multiple environments. You can each have a config file for your own environment, and put some logic into your index.php file to set the right environment according to the URL being used.

Here's a snippet from my index.php file:
Code:
/*
*---------------------------------------------------------------
* APPLICATION ENVIRONMENT
*---------------------------------------------------------------
*
* You can load different configurations depending on your
* current environment. Setting the environment also influences
* things like logging and error reporting.
*
* This can be set to anything, but default usage is:
*
*     development
*     testing
*     production
*
* NOTE: If you change these, also change the error_reporting() code below
*
*/

// Prefer HOSTNAME in the environment, which will be set in Apache.
// use `uname -n' as a backup.
$hostname = (isset($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : php_uname("n");

switch($hostname) {
    case 'dev.example.com':
        define('ENVIRONMENT', 'development');
        ini_set('display_errors', 1);
        error_reporting(E_ALL);
        break;

    case 'www.example.com':
        define('ENVIRONMENT', 'production');
        break;

    default: trigger_error('$hostname is set to an unknown value', E_USER_ERROR);
}

Now create a directory for each environment (apart from production), and each put your individual configuration files in each. CodeIgniter will load the appropriate configuration file automatically, and use those settings.

You can see more about handling multiple environments here: http://ellislab.com/codeigniter/user-gui...ments.html

Hope this helps.
#3

[eluser]merik[/eluser]
Good point. Let me explain more what exactly I want: I want the database setting (username, password, etc) NOT to be in database.php because eventually I may want to make that git repo public and don't want to expose my DB credentials. So I need the DB credentials to be loaded from a file which is not even in the /application directory and is not part of the repository. And then a similar idea with some of the configs. Is this supported?
#4

[eluser]TheFuzzy0ne[/eluser]
No, it's not supported.

I'm currently learning Git, and haven't got my head around it yet, but by default, CodeIgniter uses the 'development' environment. Shouldn't you be able to create a development directory in ./application/config and then keep all of your personal configuration stuff in there, and simply tell Git to ignore it?

If you need control over the configuration in your index.php file as well, you'd be better off maintaining your own version away from the Git project directory, and using that to bootstrap set your environment, and to set the application/system directory paths. So long as your Web server knows where to look for the index.php file, it's all good.

If you can, clone the repo outside of your Web root, and then you'd have your own index.php file inside the Web root.

Hope this makes sense. I may be talking absolute nonsense, but in my head it works. Tongue
#5

[eluser]TheFuzzy0ne[/eluser]
Another idea would be to simply setup a generic database name, username and password, so everyone can configure their database to work with it, but you may end up running into problems when it comes to supporting more than one DBMS.
#6

[eluser]Aken[/eluser]
[quote author="TheFuzzy0ne" date="1364137569"]No, it's not supported.

I'm currently learning Git, and haven't got my head around it yet, but by default, CodeIgniter uses the 'development' environment. Shouldn't you be able to create a development directory in ./application/config and then keep all of your personal configuration stuff in there, and simply tell Git to ignore it?[/quote]

This works quite well for most situations where your personal environment details should be kept secret.
#7

[eluser]merik[/eluser]
[quote author="Aken" date="1364193350"][quote author="TheFuzzy0ne" date="1364137569"]No, it's not supported.

I'm currently learning Git, and haven't got my head around it yet, but by default, CodeIgniter uses the 'development' environment. Shouldn't you be able to create a development directory in ./application/config and then keep all of your personal configuration stuff in there, and simply tell Git to ignore it?[/quote]

This works quite well for most situations where your personal environment details should be kept secret.[/quote]

Can either of you please direct me to the documentation of this development environment feature?
#8

[eluser]TheFuzzy0ne[/eluser]
Certainly. http://ellislab.com/codeigniter/user-gui...ments.html
#9

[eluser]CroNiX[/eluser]
specific config files like that should probably be added to .gitignore and not end up in the repo in that case.
#10

[eluser]Jan_1[/eluser]
Why not just feed the .gitignore-file like
Code:
*/application/config/database.php
*/config/development
*/logs/log-*.php
*/logs/!index.html
*/cache/*
*/cache/!index.html
and let an example-file in the folder like database_example.php or config_example.php with all needed info..




Theme © iAndrew 2016 - Forum software by © MyBB