CodeIgniter is an PHP framework built on the MVC design pattern. There is no doubt that it is popular, it's the most forked PHP project on Github ever, the second most watched PHP project on Github, and has a large group of followers and contributors. Why is it so well loved? The answer is that you can add your own functionality to the CodeIgniter core without even breaking a sweat. You can extend the core controllers, models, and just about everything else with relative simplicity. Let's have a look at how we can do that.
CodeIgniter allows you to extend a certain group of core classes, namely:
Before we continue, we need to understand why we would want to extend the core classes of CodeIgniter in the first place.
The reason is simple--we have a controller. Let's call it "Welcome Controller" because that is the default CodeIgniter controller. Welcome Controller is situated in a file call welcome.php in the application/controllers directory. This file is a standard CodeIgniter controller, and it is there by default when you install CodeIgniter for the first time. Welcome Controller is invoked by the CodeIgniter router when you hit the CodeIgniter landing page for the first time, and it does nothing but load the welcome_message.php view file when you hit the landing page. It does this from the index function (see Figure 3, below) which is an action method.
Now let's assume that my client has asked me to do something really silly: he wants the IP recorded every time a visitor lands on any page in the entire CodeIgniter application. There are various ways that I can do this, depending on the client's wishes, but because we know that clients don't like to waste money and we don't like to waste time, we need to do this fast, and cheap.
Human nature says all we do is create a model method that will record the IP of a user. It can record it in a database or in a file or it can email the client every time a new IP hits the site. The details of how it does this is out of the scope of this article and does not really matter. What matters, is that we want to do something every single time the page loads, no matter where we are in the application. The obvious thing to do is to chuck that model call into the constructor of every controller call, so that the IP of the visitor is recorded every time.
But what happens if our application has grown to ten, twenty or fifty controllers? Will you perhaps miss one controller? It's not hard to imagine that you could. Another point, much more important, is that you are using OOP here, for the love of Pete. Why on Earth would you want to go copy/pasting around everywhere like you learned to code last night?
Object Oriented Programming makes task this really easy for us. We have a controller (welcome.php), which extends a CodeIgniter core controller (CI_controller.) Thus we have a SUPERCLASS, which really consists of these two classes. All we need to do here is pop a class in-between the two, making the superclass bigger. Sound simple? Well, it is. Let's check it out.
If you aren't aware of the CodeIgniter application structure, it looks like this:
As you can see in Figure 1, there is a directory called "core", which is where we can extend the previously mentioned core classes. My welcome controller exists in the application/controllers/welcome.php file (see Figure 2.) This is the default directory where all controllers are placed.
Figure 3 shows the contents of my welcome.php file. Pay particular attention to line 3. Here we can see that class Welcome (the Welcome controller) extends the CI_Controller (the CodeIgniter Core Controller.)
This is where the fun begins. We need to create a new controller in the "application/core" directory. I create a file called my_controller.php and add the following code to it.
Figure 4 shows that I have created my controller which has a __construct method. The first thing we need to remember is when extending a method of a class, we need to call the parent class. Thus, line 8 of my_controller.php calls parent::__construct(). After that, we can call our model method, which will save our IP, or do whatever we need to do. Now we need to adjust the Welcome controller to extend My_controller (See Figure 5).
Figure 5 shows us that we have extended My_controller, which in turn extends CI_Controller. Everything flows through My_Controller, and that's what we want. If you want to use this same method with the model, you can. You can create a model that is extended by every model you call, in exactly the same way. In that model you can do what? Anything you want to. Connect to the DB automatically every time you call a model. Build tables. Check if a user is logged in? It's your code and you can do anything you want, very simply. All you have to do now is make sure that every controller you create extends My_controller, and not CI_Controller.
If you want to call your extended classes anything other than MY_, you need to open the file application/config/config.php, and look around line 109. You'll see something similar to Figure 6.
All you need to do here is change the "MY_" to anything you like. You'll then have to rename the file and the class accordingly, but there you have it. Some people chuck in a company name, or a library name. The choice is yours.
Remember, you can do the same with the complete list of library classes in LISTING 1, at the beginning of this article. Extending classes is a major part of Object Oriented Programming, and once you start doing it, you'll find that you can do anything you really need to do, quite simply.