CodeIgniter Controllers

Controllers take care of moving the data between the model and the views, and directing traffic
based on user actions. For example, when a user submits a form, the controller collects the data and
shuffles it off to the model, which should ensure the data is valid. The controller then tells the View
(or JavaScript layer) whether it was successful.
How Controllers Work 
Before I explain how, let’s  look at the routes.php file. This file of this  Application->Config>routes.php.
Routes.php File
when we browse http://localhost/codeigniter/, CI wil lokk for welcome controller and by default call the index method.
If we add a entry in routes.php

$route['home_page'] = 'welcome/home_page';

then browse url http://localhost/codeigniter/home_page, CI will look for a method name home_pagein Welcome controller.
Source code  of Welcome controller

defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
	public function index()

 class Welcome extends CI_Controller :  class Welcome extends(inherit)  the CI core controller.
public function index() :  This function is executed when the home page  is browsed.

Controller  Design

Let’s run through an example
This application is going to be a Customer Relationship Management software that allows you to
manage your clients, potential clients, supplier companies, and cold leads. Since we want to be able
to view and manage the individual people as well as the companies, we could start our URL mapping
with something like the following.
With this starting point, it would make sense that you have one controller called people and one
controller called companies. Inside each of those, you would have a method to handle the various
actions, using the router to allow for the object/{id}/action URLs to map to a class and method
like People->contact($id).
In addition, you would likely want some AJAX methods for adding notes to people or companies.
You might also want AJAX methods for adding reminders to the app’s calendar so that you’ll know
to follow up with the client in 3 months. I’ve seen a natural tendency to want to create a single
AJAX controller to handle all of that. However, that solution tends to create very large classes that
are difficult to maintain, and it occasionally makes it difficult to locate the proper method.
Instead, you should create separate controllers for each of the areas of concern, like notes or
reminders. In smaller applications, these controllers could hold both the AJAX and non-AJAX
methods. Once your application starts to grow, you should consider creating an AJAX directory
located at /application/controllers/ajax/ and putting any AJAX-specific controllers in that
folder. This makes everything very clear for you and any other developers. It also makes it very
simple and logical to locate the method you have to edit.
Another way to start discovering the structure of your controllers is to look at your site’s main
navigation from the designer’s mockups. This can be especially helpful if your actions are not all
based around nouns (like people or companies). You might have a client area with main navigation
focused around a Dashboard, Survey Results, and Leaderboards. In this case, you might be tempted
to simply combine those four or five pages into a single controller called clients. I think a better way
would be to create a new directory to hold several new controllers.
While it might seem at first glance that each page will only have a single method in it, you will
often find that to be untrue. The Leaderboards controller would have the method to display the
leaderboards, the index() method. It might also have additional methods to help view other aspects
of the boards, for example by a certain year, a certain client, etc. It is possible to handle all of that
with a single method and filter the results based on $_GET variables, but it is clearer to the end_user
if you handle each aspect in a different method within the same controller, because the URL will
tell them exactly what’s going on.


It also encourages you to wrap your logic up into a neat library or model so that you don’t repeat
code. Which is what you should be doing in the first place.

Recommended Tutorials for you


Anuj kumar

This is Anuj Kumar. I’m a professional web developer with 5+ years of experience. I write blogs in my free time. I love to learn new technologies and share with others. I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials. .


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button