Getting Started With CodeIgniter: Part 1 – Installation

CodeIgniter is an “Open Source Web Application Framework that helps you write kick-ass PHP programs.” The website says it all. CodeIgniter is BALLER. However, it’s documentation is awesome after you get going, but getting going is a little tougher. That’s what I’m here for. Hopefully, after this tutorial, you’ll know your way around CodeIgniter and have a pretty good feel for the MVC architecture.

In part one, we’re going to learn about installation and configuration of CI, and also get a feel for the folder structure (which always seems to scare people away). So let’s get going!

Some Background Info

For those unfamiliar with PHP frameworks or the MVC (Model, View, Controller) concept in general, here’s a little background. MVC architecture is a tried and true method of organizing source code and program files. Basically, it separates the front end from the back end.

CodeIgniter implements the MVC architecture pretty well, and then throws in TONS of PHP classes and helpers to get things done more quickly. You’ve got an image manipulation class that gives you functions to resize, crop, and watermark an image easily. You’ve got a captcha class that makes it pretty simple to generate captchas for web forms. You’ve even got a ZIP encoding class that makes it simple to create zip packages dynamically in your web site. I wouldn’t be surprised if CI 2.0 came complete with kitchen_sink.php! Shut up. That was funny.

Taking A Look Around

Now let’s take a look at CodeIgniter to see what we’re working with. Download the newest version from the CI site and extract it onto your server somewhere. Now open it up with a file browser and take a looksee!

You should see four things: A “system” folder (which contains pretty much EVERYTHING, including CI’s code and the folders to put your code into), a user guide (which you can delete because the whole thing is available on the CI website), a license.txt, and an index.php (which routes each request to wherever it needs to go.

Open up the system folder. Now, see the “application” folder? That’s your home base. Don’t touch any of the other folders in the system directory unless you’re trying to figure out how something works. As a matter of fact, don’t even do that. It’s scary. Just open the application folder. There are quite a few folders inside here. In the beginning, the only ones that you need to be worried about are config, models, views, and controllers. As you develop in your CI skills, you may develop your own libraries or helpers which go in their appropriate folders, but don’t get ahead of yourself! Patience is key! KEY!!!

Configuration and Installation

The first folder we’re going to look into is the config folder. Specifically, we’re going to mess with config.php, autoload.php, and database.php. Open up config.php and look around. This is the main place to screw around with options that CI gives you. First of all, change the value of $config['base_url'] to wherever your CI install is. If you’re running on localhost and you put it in a folder called “ci”, for example, you’ll want to use “http://localhost/ci/”. If it’s a live site, it will be something like “http://www.yoursite.com/your_ci_directory/”. Whatever it is, MAKE SURE you include the forward slash after the directory name, and MAKE SURE you include “http://”.

Moving along, we see that $config[’index_page’] is set to “index.php”. This is fine for now, because by default, CodeIgniter routes everything through the index.php page at the document route. This means that your sites will look like “www.yoursite.com/index.php/some_page”. If you want to take index.php out of the address (I always do! It’s ugly!), you can change $config[’index_page’] to an empty string (just two quotation marks with nothing in between them for those who have never ever coded before ever) instead of “index.php” and make a “.htaccess” file to remove it from the url like the CI wiki talks about.

Scoot on down to $config[’log_threshold’] = 0; and change it to 1. This will just enable error messages like PHP errors to be logged. Any lower than 1 and you won’t see any logs, any higher than 1 and your logs will fill up like crazy. So stick to 1. It’s just good practice.

UPDATE: Apparently some servers have a problem with setting log_threshold to 1, so if you get nothing but blank pages after the install, change it back to 0 and see if that fixes it. If anyone can explain this, please let me know.

UPDATE #2: Thanks to cheekygeek (see comments below), we have an answer.

A solution to the log_threshold site-breaking problem is to make the system/logs folder world-writable. However, making folders in web space world-writable (777) is never a good idea…the (more secure, I think) fix on a UNIX-based system is to change the GROUP ownership on the logs file to the web server user. If you are using Apache, you’ll find it defined in the httpd.conf.

Thanks for the info, cheekygeek!

Back to business, go ahead and change the encryption key ($config[’encryption_key’]) to a random 32 character alphanumeric string. Lots of applications like user authentication or anything to do with passwords use the encryption key by sticking it on the end of the password (it’s called a salt) and then hashing it for security. It just makes password storage a little safer.

Next we’re going to get out of config.php and move along to database.php, also in the config folder. If your application isn’t going to use the database, you can Open that one up. Most users will just have to change the first four database config options (hostname, username, password, database). If you’re not on MySQL, you’ll have to change dbdriver, and if you are prefixing all of your tables with something, you’ll have to change dbprefix. All the rest should be alright.

Finally, we’ll open up autoload.php. This mostly just tells CI which of the helpers and libraries you’d like to have automatically loaded. Helpers and libraries can be loaded on a controller by controller basis, but if there are any that you tend to use in most controllers, you might as well just autoload them. If your site uses a database whatsoever, you should probably autoload “database” in the library section. I also usually autoload “url” in the helper section. Visit the CodeIgniter user guide to see what all of these things do. You’ll probably need to spend a little while familiarizing yourself with all of these in order to take the full advantage of CI. NOTE: At the top of the User Guide, there’s a little “Table of Contents” button that brings a dropdown navigation menu. I mention it here because I spent the first two weeks banging my head on a wall until I found that.

Now fire up the browser, point it to wherever you installed CI with index.php on the end (something like http://localhost/ci/index.php). NOTE: If you used a .htaccess to take index.php out of the url, you’ll obviously won’t need to put it in the address. Did you get the welcome page? If you do, you’re rockin’ the CI! How does it feel! YESSSS! If you didn’t, post a comment here or post on the awesome CodeIgniter forums and it shouldn’t be too hard to work out.

By default, CI loads the controller “welcome.php”. To change this to your own home page, open up “routes.php” in the config folder and change the default controller. We’ll talk more about controllers (and models and views too!) in the next section so stay tuned.

This entry was posted in php and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Posted December 24, 2008 at 5:02 pm | Permalink

    Hi, this is a comment.
    To delete a comment, just log in and view the post's comments. There you will have the option to edit or delete them.

Post a Comment

Your email is never published nor shared. 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>