CodeIgniter: How to Configure the Framework

In this, the second part of my series on working with CodeIgniter, we’ll move into configuring the framework for use on a local computer or dedicated server. Since the staff at Ellis Labs has made this easy, you’ll be up and running in minutes.

If you’ve unzipped the CI zip file, as I described in my first post on CodeIgniter, you should be looking at something like this:

Code Igniter File Structure

As I said in Part 1, the first thing I do when starting to work on a CodeIgniter website is to delete the license.txt file and the user_guide folder. The next thing to do is open up the config folder. There are several files used in the base configuration. The first is autoload.php. The name is descriptive enough.

Two entries here are of interest. The first is:

$autoload['libraries'] = array();

You’ll want to add several items to this array as comma-separated strings. I usually add “database” and “session” to start. Adding session means you’ll have to add an entry to your config.php file. We’ll get to that in a moment.

The other place you’ll want to add some items is:

$autoload['helper'] = array();

Here again, helpers are entered as comma-separated strings. The helpers you’ll want to enter here are “url” and “form.” The URL helper gives you you access to functions like base_url() and site_url(), which you’ll commonly use in developing your site. The form helper, as the name suggests, gives you access to CodeIgniter’s form functionality. You can also create your own helpers, which are basically site-wide functions. Putting them in the autoload.php file means you don’t have to call the helper before you use it. That saves you some typing and makes your code cleaner and easier to read.

Next down the list is the config.php. All of the items here are part of an array called, appropriately enough, $config[]. Set your $config['base_url'] to your local host or your application’s domain. Be sure to include the trailing slash, like this:

$config['base_url'] = 'http://www.example.com/';

You’ll see the $config['index_page'] is set to index.php. You’ll want to delete that and just leave the two single quotation marks. That’s because almost no one wants index.php appearing in their URLs. If you didn’t change this, your URLs would look like this:

http://www.example.com/index.php/home/

But to completely remove index.php from the URL, you’ll also need to create an .htaccess file. I’ll tell you how to do that in a moment.

If you’ve added “session” to the autoload.php file and refresh your browser, you’ll see the message “An Error Was Encountered.” message where CI asks for the encryption key. You’ll find this further down in the file.

$config['encryption_key'] = '';

Enter something secure here. I won’t go into a discussion of security, but try to include some mixed-case letters as well as numbers and punctuation marks. Once you’ve done that, you’re finished for the moment with the config.php file.

Next is your database.php file. Fill in your host name (localhost, IP or domain), username, password and database name here:

database_settings

The other file in this folder that you’ll commonly edit is routes.php. Out of the box, you get two routes:

$route['default_controller'] = "welcome";
$route['404_override'] = '';

“Welcome” is the the controller that loads the welcome view. When you first start configuring your site, one of the primary setup steps is to change “welcome” to the name you’re going to give your default controller. I usually call mine “index_controller”. I call the view that goes with it “index_view”— both with “.php” suffixes in the file names. You don’t need to add the “.php” suffixes in CI code.

Now you can go into your controllers folder and delete welcome.php.  Do the same thing in the views folder with the welcome_message.php file.

Now to the .htaccess file. Create a file in your IDE called “.htaccess” (starting with a period). There is no extension. This is a text file that allows URLs to be rewritten without index.php. They can do a lot of other things, but for our purposes here, rewriting URLs is all we’re going to worry about.

The code that goes in this file varies from server to server. Here’s a common .htaccess:

Options FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond $1 !^(index.php)
RewriteRule ^(.*)$ /index.php/$1 [L]

This isn’t the one on the CI website, but it’s the one that’s worked best for me over the years. If this doesn’t work, use Google to find your own. You’ll know it works when you can display URLs that should have index.php in them but don’t. But note: This doesn’t count the default controller. That one displays the URL without index.php, with or without an .htaccess file.

Also note that because the name begins with a dot, it will be invisible in the Finder or Windows Explorer. You’ll be able to see it in your IDE if it is set to display invisible files. Some, not including Dreamweaver, do this by default.

Once you’ve done all this, you’re ready to start writing your first controller. We’ll do that, as well as write our first view, in the next tutorial.

If you have comments, questions or tips about CodeIgniter, be sure to share them in the comments below.

Comments

  1. BY RobS says:

    Thanks again for writing these articles. I haven’t started yet and am somewhat unfamiliar with all of this (but hoping to go in that direction) and so this was the only part that wasn’t clear:

    “$autoload['libraries'] = array();

    You’ll want to add several items to this array as comma-separated strings. I usually add “database” and “session” to start.”

    Where (and how) do I add this comma-separated list? Is it in a file? Any particular place in the file? In code? What would be the syntax, and can I put it just under that line?

  2. BY Scott Hummel says:

    The items go in the array here, as with all array config items, like this: $autoload['libraries'] = array(‘database’, ‘session’);

Post a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>