CodeIgniter is a lean PHP web application framework that is very powerful yet easy to use. It provides a set of libraries and helper functions to allow people to develop their applications much faster. For this, it uses Object Oriented Programming (OOP) techniques and a Model-View-Controller (MVC) approach for a nice separation of presentation from logic. In addition, CodeIgniter uses Clean URLs to avoid those ugly query string filled URLs that nobody likes.
This article assumes you have already installed CodeIgniter on your development environment and did the initial database and URL configurations. You can consult this tutorial for more information about quickly getting you set up.
The MVC programming approach is meant to separate presentation from logic. This means that you can have web pages without much PHP scripting as this is kept separate in other files. It's particularly useful when there are both developers and designers working on an application, the latter having a much easier time creating the front-end part.
In CodeIgniter, the Models and Controllers are basically PHP classes that you have to declare, albeit extend from the default CI ones. They are kept in separate files (each Controller or Model has its own file). The Views, on the other hand, are simple HTML pages (with a .php extension) that build up the page and only contain minimal PHP code - usually restricted to printing variables and running loops (depending on the templating engine you use). You can even include Views in other Views if you want.
You can try to think about the Model as the place where the heavy scripting happens - database queries, calculations, etc. The View is where the information gets presented by the server - the web page. And then the Controller is the intermediary - it routes the HTTP request from the client, makes use of the Model and loads the necessary Views to present the results. Pretty simple.
Using Models is not necessary in CodeIgniter, but preferable. If you don’t use them, you’ll end up cluttering your Controller methods with a lot of code and then it is more difficult to manage it. Everything depends of course on the size of your application.
So you have a Controller that uses a Model to perform some actions and display some information using a View. But how does that information get called by the client? In other words, how does CodeIgniter handle URLs?
Very simple. Assuming that you have followed the URL configuration steps mentioned in the previous tutorial, you know that the URL is built in the following way (after removing that ugly index.php segment):
This means that after the base URL (example.com) comes the class name followed by a method (class function) name, followed by the arguments you pass to this function. If the function takes more arguments, these get separated in the URL by slashes. This is a very nice and easy to understand structure.
Moreover, you have some advanced routing options to break free from this standard. Let’s say you have a simple application with the front facing site using only one Controller and method. You can make it that the respective Controller method is the default function that gets called when a user points the browser to the base URL. Additionally, you can create some rules by which whatever is passed after the base URL represents the arguments for this method. To do this, open and edit the routes.php file located in the application/config/ folder.
With a default CodeIgniter installation, you should see the following somewhere in that file:
$route['default_controller'] = "welcome";
The default_controller sets which is the Controller that gets called by default if nothing gets passed after the base URL. Change this to reflect the name of your Controller:
$route['default_controller'] = "your_controller";
After you save this file and navigate to your base URL, the index() method of your_controller should be called. This is the default method that gets called if a Controller is requested in the URL but no method is specified. Now, edit it again by adding also the method you want called by default:
$route['default_controller'] = "your_controller/your_method";
Now, if you navigate to your base URL, your_controller will call your_method. To accomplish what we set out to do though, we need to add another line to the routes.php file, just below the line where you set the default_controller:
$route['(:any)'] = 'your_controller/your_method/$
This will make sure that if you add something after the base URL, it will not be considered as a Controller but as an argument to your_method that resides in your_controller. Save the file and test it out to see. You don't need to worry about having to pass in the URL the Controller name and method. You can directly specify the arguments right after the base URL. And there are a bunch of variations here and you can play around to make some complex routings.
Note: If you are wondering about conflicts between routing rules, CodeIgniter reads the rules from top to bottom and servers the request to the first matching route.
CodeIgniter comes equipped with a lot of libraries and helper functions to make your life easier when developing your application. But unlike many other solutions out there, the entire framework remains very light because they are not all loaded from the beginning. You load them yourself as you need them in the application.
For example, to make use of the Database library (or class) in your controller, you can load it with this simple line in whichever method you need it:
Nevertheless, you also have the option to autoload libraries and helpers. This means that the application will include them from the beginning so you don’t have to bother with loading them individually in various Controllers.
The database class and the URL helper are good examples of a library and a helper that make sense autoloading as you use them quite often. The first has a very powerful Active Record pattern for engaging with the database whereas the second has some helpful functions for outputting links or loading various URLs.
To autoload the above, open the autoload.php file in the application/config/ folder and where you find the line:
$autoload['libraries'] = array();
$autoload['libraries'] = array('database');
The array needs to contain the names of the library classes you need autoloaded. Each library has to be an item in the array.
For the helpers, you need to add the helper names into this array in the same way. Replace this:
$autoload['helper'] = array();
$autoload['helper'] = array('url');
After you save the file, when your application loads, these libraries and helpers will be available everywhere. You can then also define your own libraries if you want to extend the framework.
CodeIgniter is very powerful tool with numerous ways to leverage its abilities; therefore, it is difficult to cover everything in one place. However, these are some of the most important concepts you need to know before getting started. In the next tutorial, you will get an example of how to apply all of this and create a simple application that queries the database and displays information to the browser.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.