Welcome Guest, Not a member yet? Register   Sign In
establishing a local configuration for each developer, dev server, production...
#1

(This post was last modified: 12-29-2014, 10:41 AM by sneakyimp.)

I'm looking for a great way to establish local settings in my CI working directory. The other dev on my team will also need to do this: his settings are different than mine for folder locations, db access, mail, etc. Likewise the dev server and production servers will still have different settings.

This approach looks pretty awesome:
http://caseymclaughlin.com/articles/setu...odeigniter

HOWEVER, it requires a loop of code in the application/config/database.php. Does that look OK to everyone? It looks a wee bit hackish to me but is probably acceptable because it does not go into the git repo. (EDIT: it *does* end up in the git repo and on the prod server)

If anyone has better solutions for this, I'd love to see them.
Reply
#2

I believe this is handled right in CodeIgniter 3 already.

http://www.codeigniter.com/userguide3/ge...ments.html

In my .htaccess I only need to add "SetEnv CI_ENV sandbox"

Then CI will load the configs from that environment

http://www.codeigniter.com/userguide3/li...vironments

I have been using it for over a year (using CI 3) for each of my deployment (sandbox, dev, test, production, etc...) servers.

Then as part of each boxes deploy script it does a double check on the .htaccess CI_ENV value. (git pull, db stuff, htacces stuff, etc...)

Hope that helps
Reply
#3

I actually back-ported the CI_ENV check to CI v2.2 in Bonfire so I could use this on my server. I then added the SetEnv call to a .conf file loaded by my servers (with the environment set appropriately to that server).

As an additional step, I added sub-folders in the /application/config/ directory to my project's ignore filter, so individual developers' config files don't get included in the site's repository. If necessary, the config file(s) can always be version-controlled in a separate repository.

Folder locations in the application and system directories should be relative, so it would be unusual for those to change within the project from one person to the next (though you would probably want to manage the paths in the individual development server configurations).

I do like dmyers' steps of checking the CI_ENV value for each box as part of the deploy scripts, but I prefer to keep those SetEnv calls in Apache's conf directory rather than my project's .htaccess file.
Reply
#4

(12-30-2014, 06:36 AM)dmyers Wrote: I believe this is handled right in CodeIgniter 3 already.

http://www.codeigniter.com/userguide3/ge...ments.html

In my .htaccess I only need to add "SetEnv CI_ENV sandbox"
Thanks for the information. I had considered this approach but decided not to use it because it means we have to maintain different .htaccess files on each machine OR we must add this SetEnv value in the apache configuration. I prefer to have a local config which overwrites the default config in a separate file which we can add to the .gitignore list.
Reply
#5

(12-31-2014, 10:28 AM)mwhitney Wrote: I actually back-ported the CI_ENV check to CI v2.2 in Bonfire so I could use this on my server. I then added the SetEnv call to a .conf file loaded by my servers (with the environment set appropriately to that server).

As an additional step, I added sub-folders in the /application/config/ directory to my project's ignore filter, so individual developers' config files don't get included in the site's repository. If necessary, the config file(s) can always be version-controlled in a separate repository.

Folder locations in the application and system directories should be relative, so it would be unusual for those to change within the project from one person to the next (though you would probably want to manage the paths in the individual development server configurations).

I do like dmyers' steps of checking the CI_ENV value for each box as part of the deploy scripts, but I prefer to keep those SetEnv calls in Apache's conf directory rather than my project's .htaccess file.

I like a couple of things about this approach
* SetEnv i the apache conf, not in the .htaccess file. That way, we can still have .htaccess in our repo without causing problems.
* Exclude each person's local config from the git repo so we can each keep our local settings private.

I'm still wondering about how one might also keep the production configuration secret from the devs.
Reply
#6

(12-31-2014, 11:51 AM)sneakyimp Wrote: I'm still wondering about how one might also keep the production configuration secret from the devs.

You could exclude it from the repo as well, but there are clearly situations in which they will need some information from the production server's configuration, so the relevant information (i.e. almost everything but the database password) will probably need to be documented somewhere for their reference. In fact, I believe the ignore filter in my repository includes something like "/application/config/*/database.php", which would exclude the production database config as well as individual developer configurations and other server configurations. The database.php file in the config directory itself might be a good place to document the production settings for developers to reference as needed.
Reply
#7

OK I just realized tonight that Casey M.'s method doesn't really work. It does properly override the default databse configuration, but the other settings in config.local.php are totally ignored. For instance, I set base_url in application/config.local.php like so:
Code:
$config["base_url"] = "http://example-changed.com/";
but this has no impact on the configuration when I read it from my controller like so:
Code:
echo $this->config->item("base_url");
The result of that echo statement is the same value as the primary config file, application/config/config.php. So disappointing Sad
Reply
#8

I've been exploring the loader and I think I might almost understand things well enough to set up a local PHP config file that overrides the default configuration files (but only if this file is present) but I'm confused by the fact that the various different configuration files define different variables:
config/autoload.php - defines $autoload
config/config.php - defines $config
config/database.php - defines $db
config/hooks.php - defines $hook.php
config/routes.php - defines $route

I also can't figure out how to access the db config values from $this->config->item(). From what I can tell, the db configuration values are not defined in $this->config anywhere.

Can someone suggest a way in which I might use a hook to override the default configuration values. I'm especially interested in overriding db settings, base_url, etc.

I really do NOT want to make changes to .htaccess or the apache config if it can be avoided.
Reply
#9

Why use a hook? You can define the environment inside the index.php

Why use a hook? you can simply define the environment in the index.php


PHP Code:
if(! defined('ENVIRONMENT') )
{
 
 $domain strtolower($_SERVER['HTTP_HOST']);
 
 switch($domain) {
 
   case 'www.yoursite.tld' :
 
     define('ENVIRONMENT''production');
 
   break;
 
   case 'test.yoursite.tld' :
 
     define('ENVIRONMENT''testing');
 
   break;
 
   default :
 
     define('ENVIRONMENT''development');
 
   break;
 
 }

You can find more info on environment from a post of mine: http://avenir.ro/codeigniter-tutorials/s...ironments/
Reply
#10

(01-09-2015, 09:35 PM)sneakyimp Wrote: I've been exploring the loader and I think I might almost understand things well enough to set up a local PHP config file that overrides the default configuration files (but only if this file is present) but I'm confused by the fact that the various different configuration files define different variables:
config/autoload.php - defines $autoload
config/config.php - defines $config
config/database.php - defines $db
config/hooks.php - defines $hook.php
config/routes.php - defines $route

I also can't figure out how to access the db config values from $this->config->item(). From what I can tell, the db configuration values are not defined in $this->config anywhere.

Can someone suggest a way in which I might use a hook to override the default configuration values. I'm especially interested in overriding db settings, base_url, etc.

I really do NOT want to make changes to .htaccess or the apache config if it can be avoided.

Usually the files define different variables because they are used by different parts of the system. $this->config is not intended to work with any config file which does not use the $config variable. This can be confirmed in Config's load() method, which checks whether $config is set and is an array after including the file, and exits/fails if it does not.

The loader loads the autoload config and expects it to define the $autoload variable. The database library loads the database config and expects it to define the $db variable. The router loads the routes config and expects it to define the $route variable. I think you get the picture.

In most cases, the config files will be loaded from the ENVIRONMENT directory in the config directory if a version of the file is available in that location. There are many different ways to set ENVIRONMENT, so I would recommend just picking a method which works for you, or creating your own method to do this. I wouldn't recommend a hook simply because your hooks can be set per ENVIRONMENT as well, and the only hook that fires before the config class is loaded is the pre_system hook.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB