Getting Started With CodeIgniter

CodeIgniter is a popular PHP MVC framework that’s developed a following for a variety of reasons: First, it’s very well documented. Second, there’s a lot of support for it in forums — people are familiar with it and generally happy to help. Third, there are many, many tutorials out there on how to use it. Fourth, it’s free. And fifth, it’s MVC.

CodeIgniter This is the first in a series where we’ll delve in CodeIgniter. We’ll start with an introduction to what MVC means and why we want to follow the MVC pattern, then we’ll look at CodeIgniter itself. In the next post, we’ll get into some code.

As of today, the framework is at version 2.1.3. It will probably stay there until it’s upgraded to 3.0 later this year.

What Is MVC?

MVC stands for Model-View-Controller. It’s probably the most common framework architecture out there and is found in all sorts of different frameworks, not just PHPs. Other languages’ frameworks — such as C#’s ASP.NET MVC and Ruby on Rails — use it, too.

MVC is popular because it keeps your code organized and easy to manage. It does this through what’s called the “separation of concerns.” This is a convention that dictates where particular types of code should be housed. The idea is that there should be as little repeated code as possible and very little duplication between concerns.

CodeIgniter and other frameworks do this by delineating concerns (code) into models, views and controllers. Models are the representations of your data, including all of your database calls and business logic. Views are renderings of the data, as directed by the controller. If it’s HTML, CSS or JavaScript, it should be in your view (with some exceptions for DHMTL). Finally, the controller is the “traffic cop” that directs the flow of information, making database calls and sending data to the view.

Why Use a Framework?

True, you can create your own bare-bones MVC website without a framework. The question is, why would you do that when there are so many frameworks out there that can do all of the heaving lifting for you? They keep the code you have to actually write to a minimum. This is especially valuable if you’re new to a language.

Trained teams of developers have spent many hours writing and updating these frameworks. Writing your own MVC website without one would be time-intensive and means that you’d have to code from scratch not only your controllers, but also the code that loads the controllers, as well. And so on. These frameworks exist because people got sick of writing the same core functionality over and over again.

So why not let someone else do the work for you? If you try out CodeIgniter and decide you don’t like it, you’ll find plenty of other PHP frameworks out there to make your life easier. There are really too many to analyze here, but some of the more popular ones are Zend, Symfony, Laravel, CakePHP, FuelPHP and Yii. Also there’s Laravel, which has been particularly popular recently, although it’s quite new.

On to CodeIgniter

Now that we’ve covered MVC and why we want to use a framework, let’s move on to the main event. Go ahead and download CodeIgniter from the Ellis Labs website and open up the ZIP file. You’ll have a folder with three sub-folders and two files in it.

You can delete two things immediately: the user guide folder and the license.txt file. The user guide is available on the website, so this folder is only useful if you are working locally and don’t have Internet access. The license file is for your information only and generally isn’t uploaded to a server.

What remains are the application and system folders as well as the index.php file.

The first rule of CodeIgniter is don’t touch the system folder. That’s where all of the magic happens. You don’t want to mess with it, especially if you don’t know what you’re doing. If you need to alter or extend the framework, do that in the application folder — CI is fully extensible.

All of your code will go into the application folder. Open it up and you will find a number of sub-folders, including folders for your models, views and controllers. These last three are logically named and tell you where to put your files, unlike some other PHP frameworks, which use slightly different conventions.

The other folder to concern yourself with immediately is the config folder. As the name suggests, this contains all of the settings for configuring your project. There is a config.php file for basic settings and a database.php for your database settings. Routes go in the routes.php file. (We’ll cover what routes are and why we use them in a later tutorial.)

If you load your CI folder in a browser (from MAMP or WAMP or wherever you have PHP installed), you’ll see the welcome screen. This is functionality you get out of the box, unlike some other frameworks. For instance, Symfony requires some configuration before you get a welcome screen, while Laravel first requires you to change folder permissions. These aren’t onerous tasks, but it’s nice that CI does them without any configuration.

In the next tutorial, we’ll get started on our first project. First, we’ll do some configuration of CI. Then we’ll construct our first controller and have it render some text to a view. We’ll also take a look at routes so that we can begin to build our first project.

In the meantime, if you’ve got any questions for me, just post them in the comments below.

Comments

  1. BY RobS says:

    I love the idea of this.
    Will future articles reference back to previous articles in case I miss one?

    • BY Mark Feffer says:

      Hi, Rob – Yes, we’ll cross-link them.

    • BY RobS says:

      On another note, I “loaded my CI folder in a [Firefox] browser” but only go a directory listing. If I load the index.php, I get nothing.
      I assume there’s a prerequisite that, for example, I have to be running on a Linux box with PHP installed? My Win7 system doesn’t have php. If I load everything on my domain server and connect to the index, will it show me something? I guess I missed something.

  2. BY Scott Hummel says:

    I updated the article. I meant from wherever you have PHP installed. Use WAMP on a Win7 system to run a PHP application. Here’s the link to WAMP: http://www.wampserver.com/en/

  3. BY Jake says:

    Nice job, Scott! People like you are part of what makes CodeIgniter so awesome… community support! Thanks for continuing the education of developers in the power, simplicity and elegance of the CodeIgniter PHP Framework. Two thumbs up!

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>