How to Conditionally Load Configuration Files Within CodeIgniter?

Active3 hr before
Viewed126 times

8 Answers


Note: CodeIgniter always tries to load the configuration files for the current environment first. If the file does not exist, the global config file (i.e., the one in application/config/) is loaded. This means you are not obligated to place all of your configuration files in an environment folder − only the files that change per environment.,Now, my_config.php will be autoloaded. From there, if it is production server, config_production.php will be loaded; else config_development.php will be loaded.,When do you need the config initialised by? - could you not define a hook to load up the correct configuration once everything else had been initialised but before the controller had executed?,Using this as a starting point will let you do the exact same thing for database.php, etc. -- any /config/ file you want an environment specific version for without writing your own envrionment-loading logic that CI already provides.

Two options: You can try referencing the object with "$CI" instead of "$this":

$CI = & get_instance(); //do this only once in this file
$CI - > config - > load();
$CI - > whatever;

Or secondly, you could switch configs from within your config.php file:

< ? php
if (gethostbyaddr("") == 'hello.sabya') {
   $config['base_url'] = "";
} else {
   $config['base_url'] = "";

} ?
load more v

CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications.

& lt; ? php
if (gethostbyaddr("") == 'hello.sabya') {
   $this - > config - > load('config_production');
} else {
   $this - > config - > load('config_development');
} ?
& gt;
load more v

I headed to the CI docs and found one paragraph about environment-specific configuration files in the Config class docs. Not exactly prime real estate if you want to get noticed…,Instead of having to ignore configuration files in Git to handle the differences in production vs. development, now all that is necessary is a conditional to handle the variations.,Not only is it great for setting up server configurations like error reporting, it’s perfect for use in config files as well.,Fast-forward a year, all the way to yesterday, and I happened upon another great little feature: “Environment-specific configuration files.” It was really by accident that I stumbled on it and only did because I was searching for ENVIRONMENT in my full CI project.

About a year ago, CodeIgniter got the addition of an ENVIRONMENT constant which saved me a lot of headaches with managing difference between my various environments (development, staging, production).

// Conditional I add to index.php to self-determine the environment
if ($_SERVER['SERVER_NAME'] == '') {
   define('ENVIRONMENT', 'development');
} else {
   define('ENVIRONMENT', 'production');

if (defined('ENVIRONMENT')) {
   switch (ENVIRONMENT) {
      case 'development':
      case 'testing':
      case 'production':
         exit('The application environment is not set correctly.');
load more v

If the value doesn’t match a valid locale as defined in the App configuration file, the default locale will be used in it’s place.,You only need to provide values for those messages that would be localized differently for that locale variant. Any missing message definitions will be automatically pulled from the main locale settings.,Every site will have a default language/locale they operate in. This can be set in Config/App.php:,In this example, if the user tried to visit, then the locale would be set to fr, assuming it was configured as a valid locale.

/app /
app.php /
load more v

The first step in developing with CodeIgniter is to configure it. This is accomplished by modifying the appropriate configuration file located in the config/ folder.,The default controller,Before you get into working with CodeIgniter, it is important to review some of the concepts CodeIgniter uses.,If you?re developing with CodeIgniter, setup takes about 10 minutes. Here?s what you need to do:

Listing 1: LogActions.vbs

' logactions.vbs
' Get the name of the computer they logged in to.

Set loS = _

strComputerName = _

' The specific event is passed through by the Group Policy
strEvent = WScript.Arguments.Item(0)

' The user name
Set loNW = _
strUserName = loNW.UserName

' The connection string
strConn = "Driver={MySQL ODBC 5.2a Driver}; "
"Server=localhost; Database=userlogs; " & amp;
   "UID=root; PWD="

strQuery = "INSERT INTO logs " & amp;
   "(cusername, cevent, ccomputer) "
VALUES('" &amp; strUserName &amp; "', '" _ &
      amp; strEvent & amp;
      "', '" & amp; _ strComputerName & amp;

      ' Book the event in the database
      Set loConn = CreateObject("ADODB.Connection") loConn.Open strConn loConn.Execute strQuery loConn.close
load more v


Fixed a possible file inclusion vulnerability in Loader Library method vars().,Fixed an XSS vulnerability in Security Library method xss_clean().,Fixed a number of new vulnerabilities in Security Library method xss_clean().,Security Fixed a number of new vulnerabilities in Security Library method xss_clean().

load more v

Set apm_non_local_traffic: true in the apm_config section of your main datadog.yaml configuration file.,Operator configuration,Monitor Configuration,Advanced Configuration

# using RPM package(RHEL / Centos 6 + , Fedora 20 + )
rpm - ivh datadog - php - tracer.rpm

# using DEB package(Debian Jessie + , Ubuntu 14.04 + on supported PHP versions)
dpkg - i datadog - php - tracer.deb

# using APK package(Alpine)
apk add datadog - php - tracer.apk--allow - untrusted
load more v

Other "files-conditionally" queries related to "How to Conditionally Load Configuration Files Within CodeIgniter?"