How to Conditionally Load Configuration Files Within CodeIgniter?

Asked
Active3 hr before
Viewed126 times

8 Answers

filesconditionally
90%

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("127.0.0.1") == 'hello.sabya') {
   $config['base_url'] = "http://dev.example.com/";
} else {
   $config['base_url'] = "http://prod.example.com/";

} ?
>
load more v
88%

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("127.0.0.1") == 'hello.sabya') {
   $this - > config - > load('config_production');
} else {
   $this - > config - > load('config_development');
} ?
& gt;
load more v
72%

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'] == 'mysite.dev') {
   define('ENVIRONMENT', 'development');
} else {
   define('ENVIRONMENT', 'production');
}

if (defined('ENVIRONMENT')) {
   switch (ENVIRONMENT) {
      case 'development':
         error_reporting(-1);
         break;
      case 'testing':
      case 'production':
         error_reporting(0);
         break;
      default:
         exit('The application environment is not set correctly.');
   }
}
load more v
65%

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 http://example.com/fr/books, then the locale would be set to fr, assuming it was configured as a valid locale.

/app /
Language
   /
   en
app.php /
   fr
app.php
load more v
75%

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 = _
WScript.CreateObject("WScript.Shell")

strComputerName = _
loS.ExpandEnvironmentStrings("%COMPUTERNAME%")

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

' The user name
Set loNW = _
WScript.CreateObject("WScript.Network")
strUserName = loNW.UserName

' The connection string
strConn = "Driver={MySQL ODBC 5.2a Driver}; "
_
   &
   amp;
"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
40%

0
22%

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
60%

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?"