In this mini-series I'll be discussing how and why you should use the config.php file to customise your ExpressionEngine installation. In Part 1 I covered the basics, explained why using a config file was helpful and showed you a simple example config file.
Today, I'm going to take a look at making your config.php file much more dynamic. Using this file and a clever bit of PHP code, we can make EE deployment much, much easier.
The very basics of developing and deploying a dynamic website involve setting up various environments in your code. CodeIgniter 2 Core, upon which EE is based, doesn't include environments by default, so we'll have to handle this on our own. CodeIgniter Reactor does, so be sure to check that out.
I set up the domain of my development environment (using virtual hosts with a tool like MAMP Pro) to be the domain itself with the .local extension. I can check to see if I'm running on my local server by checking if the $_SERVER['SERVER_ADDR'] matches 127.0.0.1, the localhost loopback IP.
Add this at the top of your config file:
/* Dynamic Configuration
-------------------------------------------------------------------*/
$ext = ($_SERVER['SERVER_ADDR'] == '127.0.0.1') ? '.local' : '.com';
$admin_url = 'http://admin.mysite'.$ext;
$base_url = 'http://mysite'.$ext;
This switches an $ext variable between .local, the localhost or .com, the live server. I'm also setting a temporary admin URL variable here to be a subdomain. This can change; I prefer to move the system directory out of the web root and set it up as a subdomain. Learn more about using a Masked CP at the EE docs.
What if you're working with multiple servers? Using server variables, we can easily figure out the URL of the current server and use it as a catch-all. Unless you need to do real specific configuration for an environment, this can work well.
$base_url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$base_url .= "://".$_SERVER['HTTP_HOST'];
Now we have these dynamic variables, we can start to use them to make our config values more dynamic:
$config['site_url'] = $base_url;
$config['cp_url'] = $admin_url;
$config['theme_folder_url'] = $admin_url . '/themes';
Our site URL, CP URL and theme folder URL are now all being set relative to the current environment's URL. This means that we can move our install from our local environment to our live environment without having to fear; the URLs will automagically update themselves.
There are a few more URLs and paths we need to handle.
$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/";
This is really all it takes to create a dynamic site in ExpressionEngine. The only thing left to do is make our database connecting dynamic. We can do this by checking our base URL in our database.php file.
$local = (bool)(config_item('base_url') == 'http://mysite.local');
$db['expressionengine']['hostname'] = "localhost";
$db['expressionengine']['username'] = ($local) ? "root" : "live_db_user";
$db['expressionengine']['password'] = ($local) ? "root" : "live_db_password";
$db['expressionengine']['database'] = ($local) ? "mysite" : "live_db_name";
That was extremely simple to do, but very, very powerful. Our database connection will update itself correctly as our site flicks between local and live servers.
This is great so far, but our config setup isn't quite perfect. In the next article, we'll look at both tidying up your config.php file, and some other cool things you can do.