Posted 07.28.2010

Posted 07.28.2010

Getting Started with CodeIgniter

Note:

This post assumes that you have basic understanding of PHP as well as a server and MySQL database that you can work off. If not and you’re on a Mac, MAMP is a great place to get started. Otherwise, you can look at setting up hosting at GoDaddy or MediaTemple, or any other hosting companies online with PHP and MySQL enabled.


INSTALLING CODE IGNITER

  1. Go to codeigniter.com and download the most recent set of files.
  2. Once it’s downloaded, you can unzip it and place the files on your server.
  3. Magic!! —well, there a few more things you might want to do, but for the most part, that’s it.


WALKING THROUGH THE FILES + FOLDERS

When you unzip the file, I’m sure you noticed, there are really only 4 items. Not intimidating at all.

Default File Structure

The user_guide folder contains all the documentation you need. If there’s one thing Ellis Labs does right is documentation. Technically, you don’t need this folder on your server, but I enjoy keeping it locally if I need to look something up and I don’t have access to the Internet. Otherwise, you can just use the documentation on CodeIgniter’s site.

The system folder contains all the files needed to make CodeIgniter (CI) work. This is where you’ll be spending a majority of your time.

One thing that confused me, when I got ready to build my site: Where do I put my images, css files, and javascript files? Right, wrong, or indifferent, I pull all folders in the root folder of my site. My directory ends up looking like this:

Other folders in structure

Let’s dig through the system folder a little more. Don’t worry, it’s not as complicated as it looks at first glance.

  • application This folder contains all the files specific to one application.
    • config This folder contains all the site’s configurations.  Generally, you just touch this once and then leave it alone.
    • controllers Remember earlier this week when I talked about the C in MVC? Controllers are the piece that talk control what you look at. All those files go here.
    • errors This folder contains all the templates for your error pages.
    • helpers This folder contains your application’s helper files. Helper files are a collection of functions for a particular category. They help you with tasks. For example, one of my favorite helpers is the form helper. Instead of typing
      <form method="post" action="http:/example.com/index.php/email/send" />

      I can use a helper function instead and it will write the code for me

      echo form_open('email/send');
    • hooks This folder contains all the hooks for modifying CI’s core files. What’s a hook? Well, think of it as a cue. There’s a specific execution process. (If you’re interested, check out the Application Flow in CI’s documentation.) A hook, would interrupt that process so that you could step in and run one of your scripts.
    • language This folder contains all the files for making a multilingual site.
    • libraries This folder contains all the libraries specific to your application.  Honestly, I get libraries and helpers mixed up. The difference is that libraries are utility classes where object state is important (authentication, etc.) Helpers are collections of related functions (not classes) that do repetitive tasks. (I’m quoting bretticus’s response on the forum)
    • models As the name suggests, this folder contains all your models, or files that talk to the database.
    • views This folder contains all the views, or v in MVC. These are the pieces of the site that the user actually sees.
  • cache This folder contains all the cache files.
  • codeigniter This folder contains all of Ellis Lab’s files that make CI work.
  • database This folder contains CI core database files that enable you to connect to the database.
  • fonts This folder stores fonts
  • helpers This folder contains all of Code Igniter’s helper files
  • language Similar to the files inside, application > languages, this folder contains language files that allow your site to be multilingual
  • libraries This folder contains all of CI’s core library files
  • logs This folder contains any logs generated by CI.
  • plugins This folder contains any plugins for your site. These are generally files that other people have created that you can download and implement in your own application.
  • scaffolding This folder contains all the files that allow scaffolding. I know it’s a funny term. Basically, it allows you to easily add, edit, and delete entries from the database.

I know this seems like a lot of information, but really, you’ll spend most of your time in only 3 folders
1. system > application > controllers
2. system > application > models
3. system > application > views


Setting up the config file

Go to your system > application > config > config.php file and edit the base_url to wherever you’ve placed your files.

config.php in CI

I have everything running locally on my computer in a folder called awesome.

CI config URL


Making sure CI is working

Open up your web browser of choice and go to your URL. If everything’s running properly, you should see something like this:

CI Success


Setting up the database

Database structure and naming conventions
The cool thing about CI is that it allows you to dictate your own database structure. When I created my database, I used some of Cake’s conventions…they seem to help quite nicely.

For example, all my tables begin with ci_. You can guess what ci stands for. grin

The rest of the table name is plural, all one word. So, a table of tasks is citasks or a table of people is cipeople. Pretty obvious. If I have a relationship table (a table that connects tasks and people) then, I name it cipeopletasks. I separate the two items with an underscore and list people first because p comes first in the alphabet.

Redundant, maybe, but another example: blog posts and related comments = three tables. ciblogposts, cicomments, and ciblogpostscomments.

These are small things, but help tremendously when I’m working on my site. Not only is it easier to remember table names, but I know from glancing at the name whether it’s a relationship table or a regular table.

If I’m over your head…
I would recommend checking out Sitepoint’s book, Build Your Own Database Driven Web site using PHP and MySQl.. Honestly, that’s how I got my start. Kevin Yank, the author does a fantastic job of explaining things in a way that me, a graphic designer can understand.

Working on your database
There are a few things I use so that I don’t have to build my database in the command line. I have phpMyAdmin installed on my server. —which is fantastic. I’ve also used Sequel Pro, but my personal favorite is Navicat. In fact, Navicat has a feature for syncing your database with your computer and your server, if you build your site locally. The light version is free, but doesn’t include syncing. The paid version is worth every penny, though.

Getting CI and your database to talk
Open system > application > config > database.php

CI configuring the database

Scroll down to line 40 and change the values. If you’re hosting the database on your machine, chances are you won’t change the hostname, it will stay localhost. Even if you’ve uploaded the files to your server, usually this will stay “localhost.”

On line 41, add your username, line 42 your password, and line 43 the name of your database.

Bam! Magic!


Done.

That’s it. Honestly. Now, we get to have fun.

I’m working on a series of blog posts to continue out of this one that will walk through creating your own blogging CMS. I know there are plenty of blogging platforms out there. In fact, I would recommend using WordPress or Tumblr or Expression Engine before we reinvent the wheel, but I thought it would be a good exercise in building a CMS / Application. The functionality is basic. There aren’t very many decisions that need to be made and it should give you everything you need to know to build an application of your own in CI. —Giddy up!

 


  • http://www.arboractive.com Mike Atwell

    Thank you very much for sharing this information!!! Additionally, the tone of your tutorial is delightful.