Getting to know ExpressionEngine 2's config file - Part 3
Saturday, November 5, 2011 at 1:09PM This is the belated finale in the trilogy of articles I'm writing about how to customise your ExpressionEngine config.php file to allow for a much more dynamic and customisable ExpressionEngine install.
In Part 1, we looked at the basics, why and how to use the config file most effectively and we built a simple example. In Part 2, we extended our config file to allow for multiple servers without changing any values. Today, in Part 3, we'll look at tidying up our config file and we'll take a look at some of the extensive hidden configuration variables.
Let's take a look at our config file so far:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Dynamic Configuration
-------------------------------------------------------------------*/
$ext = ($_SERVER['SERVER_ADDR'] == '127.0.0.1') ? ‘.local’ : ‘.com’;
$base_url = $base_url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$base_url .= "://".$_SERVER['HTTP_HOST'].$ext;
$admin_url = $base_url . '/admin.php';
/* ExpressionEngine Configuration
-------------------------------------------------------------------*/
$config['app_version'] = "213";
$config['license_number'] = "0000-0000-0000-0000";
$config['debug'] = "1";
$config['install_lock'] = "";
$config['system_folder'] = "system";
$config['is_system_on'] = "y";
$config['allow_extensions'] = "y";
$config['site_url'] = $base_url;
$config['server_path'] = FCPATH;
$config['cp_url'] = $admin_url;
$config['theme_folder_url'] = $config['site_url']."/themes/";
$config['theme_folder_path'] = $config['server_path']."/themes/";
$config['save_tmpl_files'] = "y";
$config['tmpl_file_basepath'] = $config['server_path']."/templates/";
$config['avatar_url'] = $base_url."/uploads/system/avatars/";
$config['avatar_path'] = $config['server_path']."/uploads/system/avatars/";
$config['photo_url'] = $base_url."/uploads/system/member_photos/";
$config['photo_path'] = $config['server_path']."/uploads/system/member_photos/";
$config['sig_img_url'] = $base_url."/uploads/system/signature_attachments/";
$config['sig_img_path'] = $config['server_path']."/uploads/system/signature_attachments/";
$config['prv_msg_upload_path'] = $config['server_path']."/uploads/system/pm_attachments/";
/* CodeIgniter Configuration
-------------------------------------------------------------------*/
$config['base_url'] = $config['site_url'];
$config['uri_protocol'] = 'AUTO';
$config['language'] = 'english';
$config['charset'] = 'UTF-8';
$config['subclass_prefix'] = 'EE_';
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\\-';
$config['enable_query_strings'] = FALSE;
$config['directory_trigger'] = 'D';
$config['controller_trigger'] = 'C';
$config['function_trigger'] = 'M';
$config['log_threshold'] = 0;
$config['log_path'] = '';
$config['log_date_format'] = 'Y-m-d H:i:s';
$config['time_reference'] = 'local';
/* End of file config.php */
/* Location: ./system/expressionengine/config/config.php */
This is already nice and tidy. But it'd be great to consolidate our database information in here too, wouldn't it? That way, we can change all our system configuration in one file. This is how we do it:
/* Universal database connection settings
-------------------------------------------------------------------*/
$active_group = $ext;
$active_record = TRUE;
$db['.local']['hostname'] = 'localhost';
$db['.local']['username'] = 'root';
$db['.local']['password'] = 'root';
$db['.local']['database'] = 'some_database';
$db['.local']['dbprefix'] = "exp_";
$db['.com']['hostname'] = 'localhost';
$db['.com']['username'] = 'user';
$db['.com']['password'] = 'password';
$db['.com']['database'] = 'database';
$db['.com']['dbprefix'] = "exp_";
$db[$active_group]['dbdriver'] = "mysql";
$db[$active_group]['pconnect'] = FALSE;
$db[$active_group]['swap_pre'] = "exp_";
$db[$active_group]['db_debug'] = FALSE;
$db[$active_group]['cache_on'] = FALSE;
$db[$active_group]['autoinit'] = FALSE;
$db[$active_group]['char_set'] = "utf8";
$db[$active_group]['dbcollat'] = "utf8_general_ci";
$db[$active_group]['cachedir'] = $config['server_path'].$config['system_folder']."/expressionengine/cache/db_cache/";
By extracting out the majority of the variables (the ones that probably won't change depending on environment), and then using the $ext variable to switch the $active_group, we can adjust the database connection settings automatically depending on environment. There's one more thing we need to do. Open up your config/database.php file, empty it, and give it a solitary line:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require 'config.php';
/* End of file database.php */
/* Location: ./system/expressionengine/config/database.php */
Now, all references to the database configuration will be brought through the DB config file from the config.php file.
Alongside the standard configuration - paths, debug settings, URLs etc - we can customise a lot of ExpressionEngine's behaviour through a little-known technique called hidden configuration variables. A quick TextMate project search for 'Hidden Configuration Variable' bears 33 different variables with which to customise behaviour. Let's look at a couple of them:
$config['allow_textarea_tabs'] = 'y';
If you wish to preserve tabs in textareas, or let the user add a tab character, rather than shift focus to the next input, you can set this config item and all textareas in MarkItUp mode will preserve their tabs. Turning it off is as simple as setting it to 'n'.
Is the automatic JavaScript login reminder in the CP annoying you? Turn it off:
$config['login_reminder'] = 'n';
Do you want your formatted text links to automatically open in a new window?
$config['popup_link'] = 'y';
I'm sure you get the idea. There's a comprehensive list of the hidden configuration variables in the user guide.
I hope that this series has taught you one or two things about customising and using your config.php file to best effect. It's a fantastically powerful weapon to have in your arsenal, and it should make creating dynamic websites with ExpressionEngine much simpler.
expressionengine 