Last week I wrote about a small CodeIgniter app called Simple Task Board. It is capable of managing multiple projects and tasks, but it is very simple, specially now that it is in the first version. So I thought it would be a good idea to explain how it was done. This explanation will occur in a series of posts, starting today with an introduction about CodeIgniter and about this specific application.
CodeIgniter is a MVC (model-view-controller) Framework for PHP with a small footprint. This means that it features a complete set of functions that helps you to organize and speed up the development process of web applications. Besides, it is quite simple when compared to some other frameworks.
The community around a framework is also something to take in consideration when choosing one. CodeIgniter has an active community and a good user guide, so it is easy to find the information you need. Now let me explain what MVC means.
It is design pattern that divides the application in three parts:
- Model: database interaction and business logic. CRUD (create, read, update, delete) operations.
- View: what the user sees. Our HTML.
- Controller: application logic and user input handling. The controller uses the model to get/set the information in the database and calls the necessary views to display it to the user.
Simply explained, it is an architecture or model to structure your application. For those who have already worked with pure PHP applications that handle inputs in the same file where the HTML is, includes the database connection and executes the database actions, working with a MVC framework is awesome. At first it might look difficult, but after a few moments understanding the simplicity and organization of the code, you won’t work without it again.
Structure and Files
The CodeIgniter installation has a standard folder structure. This can be changed by tweaking a few files, but for the purpose of this application we will keep the folder structure as it is. So go to http://www.codeigniter.com and download the latest version (right now 2.1.0). Unzip the contents into your desired folder. This is what you’ll see:
The file index.php indicates the environment (development, production, etc) and the system and application folders. So, if you want, you can rename this folders for something else and change this value in the index.php file. This helps improving the security of your app.
The file license.txt contains the legal agreement for the use of CodeIgniter.
This is the folder where all information specific to our application will go. The most important subfolders, where you will doing most of your work, are:
- config: the app configuration. Database settings, autoload libraries and helpers, routes, etc.
- controllers: app controllers. Each controller is a PHP class that extends the CI_Controller class. To access the controller you simply go to server/controller_name in your browser.
- models: database models. The models are also classes, but they extend the CI_Model class.
- views: all your views will be here.
There are many other subfolders, like libraries and helpers, where you can add your custom classes and functions.
This folder contains the “engine” of CodeIgniter. All the system libraries are here, so you don’t need to worry about it.
As you can imagine this is the user guide that comes with CodeIgniter. You can remove this folder or keep it in a separate place for reference. You can find this user guide online as well.
Simple App Flow
This is a simplified flow to explain how a simple scenario works.
In this scenario we are accessing a user to edit his details. CodeIgniter handles URLs in a simple way, using the first part as the controllers and the next one as the method. Any other values can be parameters of the method. If the controller is not defined the default one will be loaded (this is defined in the config folder). If the method is not defined, the index method will be used by default.
I will explain in detail how we do everything in the next posts. My intention is to go step by step, explaining how CodeIgniter works in practical situations. Now let me explain what the application will be.
Simple Task Board
The application we will be creating is the Simple Task Board. It is available here in this blog as well as in GitHub. It was created by myself, to help me organizing my personal projects. I decided to make it available in order to improve it and, maybe, use it to help me manage projects in my company. As I don’t need and don’t want to loose to much time managing the projects, I don’t need many features. I end up using an agile board or simple spread sheets. That’s why the Simple Task Board could be tailored to support only the basic needs, being at the same time simple and useful.
That’s all for the introduction. I’ve prepared a new installation of CodeIgniter in my local machine, so I’m ready to start the step by step tutorial. Next weekend I’ll prepare part 2 of the tutorial. Have a nice week.