HMVC: Modular applications in CodeIgniter

Today we will make an introduction to how to create modular applications in the framework CodeIgniter using the HMVC extension (Hierarchical Model View Controller), so that our application gains in flexibility and we can split the project in parts that could be developed in parallel between multiple developers once the modules have been designed, with the valuable time saved to finish the project. HMVC allows us to have the same functionality offered by the “Bundles” in Symfony2 or the modules in the ZendFramework2.

Installing HMVC in CodeIgniter

The first thing we need to do in order to modularize our application is to download the HMVC extension. Once we unzip it, we find two folders inside:

core
third_party

We put the “third_party” folder (as it is) inside the “application” folder of our CodeIgniter application. The final location of the core folder depends on the version of CodeIgniter that we have installed. Therefore, the contents of the folder “core” will be in:

application/core (if we have CodeIgniter 2.0 or superior)
If we have CodeIgniter 1.7.3:
into application/libraries we will put the content of the “core” folder (3 files)

Well, with this we have installed the extension that allows us to modularize our application, but we need a final step. Inside the “application” folder we must create a directory called “modules” (application/modules). This directory (as its name suggests) will contain all the modules that we will develop.
[…]

JavaScript and CSS minified with Carabiner in CodeIgniter

Today we will talk about a powerful library available for the framework CodeIgniter, it is: Carabiner. This library allows us to include specifically for each view of our site the JavaScript and CSS files that are needed, avoiding to have to include them all at once, and also it sends them to the user in only one single file .”js” and one “.css” with all that we have including before but minified, so the size of the “.js” and “.css” that are sent to the user is much lower, with the improvement of loading time of the site that this implies.

Installing Carabiner in our project

The first thing we need for installing Carabiner in our CodeIgniter project is to download it. We can download it from the CodeIgniter forum. In order to install it, we simply have to place the files “carabiner.php”, “jsmin.php” and “cssmin.php” files into the directory /application/libraries of your project. Once this is done, we have to place the configuration file “carabiner.php” into the directory /application/config. In this file we have to specify the path to the directories that contain our files “.js” and “.css”.
[…]

Upload progress bar in CodeIgniter without Flash

Today we will see how we can create a progress bar file upload in CodeIgniter without the need for libraries or plug-ins made with Flash (like SWFUpload) that are beyond our control, because usually these libraries offer us already compiled .swf files, so we can not change anything in them, just in case we have knowledge of programming in ActionScript 2 or 3.

First of all, we have to say that creating a progress bar file upload in PHP is not as easy as it seems. The first problem is that PHP versions prior to 5.2 do not provide the necessary tools to provide information about the file upload at every moment. The second problem is that AJAX, by itself, does not allow us to check the status of the file upload process, since, for obvious security reasons, JavaScript does not have access to files on the client operating system, so you need a “trick” using an iframe.
[…]

SWFUpload in CodeIgniter

In this post we will explain a little what it is and how we can use SWFUpload library in our application made with the php framework CodeIgniter, so that, our users can upload their files to the application in an elegant and visual way, and all combined with safety and ease that CodeIgniter offers for uploading files to the server.

What is SWFUpload?

SWFUpload is a library that allows to our website’s users to upload files to the server, using a combination of Flash and JavaScript.
[…]

Sending forms with AJAX in CodeIgniter

Today we are going to explain how we can send a form via AJAX using jQuery in CodeIgniter, so we avoid to reload the whole page, giving the appearance of “desktop application” to our web application, being able to perform certain actions without reloading the entire page view.

Creating the form

First, we need to create our form. In this case we will create a generic form, where we will add different types of fields to make the example as complete as possible, but functionally not very helpful, mainly for teaching purposes… So in our view file in CodeIgniter we add the following code:
[…]

PHPMailer in CodeIgniter

In a previous post I talked about how to use the library PHPMailer to send emails from our website without having our own mail server running on the machine. This time we will go a step further and explain how to integrate this library in our CodeIgniter project and not die in the attempt.

The first thing we do, if we have not done it yet, will be to download the library. Once we unzip the library, we can rename the folder named PHPMailer and move it to the folder /system/application/libraries (for CodeIgniter 1.7.3 or previous) or to the folder /application/libraries/ (for CodeIgniter 2.0 or later) in our CodeIgniter application. Once we did it so, we can start to implement our own library, which will be in charge of including PHPMailer into our application. To do this we have to create a new PHP file in the CodeIgniter’s library directory called my_phpmailer.php. In this file we will write the following code:
[…]

CodeIgniter and JSON with jQuery

In this post, we are going to talk about how to receive data in CodeIgniter via JSON, using for that the JavaScript framework jQuery. But before that, we need to clarify a bit what is JSON and what is it used for.

What is JSON? What is it used for?

JSON stands for JavaScript Object Notation. So, it is a way of writing the properties of objects in JavaScript so that they can be sent between client and server and vice versa. This format is very light due to its internal structure, which is quite human readable, and it is very easy to process by machines. While there is a debate about which format is better, if XML or JSON, for sending and receiving data via AJAX, there is a large community of developers who bet on JSON as the AJAX communication technology that will prevail in the future, alleging among other things that being lighter than XML the communication between client and server is faster. […]

Automatic configuration of URL in CodeIgniter

In this mini-post we are going to explain how to configure our CodeIgniter in order to make it run on any server without having to be modifying the parameter “base_url” in the configuration. So, we can move our development version of our site to another different server (let’s say, the production server), without worrying about this parameter.

Inheritance between controllers in Codeigniter

Today, we will talk about the inheritance between controllers in CodeIgniter. At first glance, it could seem very simple to do and it should not be more complicated than this:

class Parent_controller extends Controller {

    function __construct()
    {
       parent::Controller();
       ...
    }
    ...
}

class Child_controller extends Parent_controller {
    function __construct()
    {
        parent::__construct();
        ...
    }
    ...
}

If we run this code as it is, CodeIgniter will return an error saying that it can not find Parent_controller class, which I founded quite surprising. […]

Adding AJAX to Codeigniter with jQuery

Today we will talk about adding AJAX to the PHP development framework called CodeIgniter. When I started to work with this framework I found rather strange that there was not too much information about how to integrate AJAX into it, since it is a relatively known framework and AJAX methodology is very common in web development. When I had the need to integrate AJAX using the JavaScript framework jQuery into my application made with CodeIgniter, I have been looking for information about this subject on the Internet and the truth is that, besides being very scattered, it is not very abundant and not very clear, so here it is my contribution from what I have learned, hopefully useful. […]