Web Programming TutorialsControllers and their use in CodeIgniter PART-1

Controllers and their use in CodeIgniter PART-1

Today we will be learning the use of controllers in codeigniter in this Controllers and their use in CodeIgniter tutorial.

We all know that codeigniter follows the MVC architecture which works with Models, Views and Controllers. All the three entities viz. model, view and controller are given separate tasks.

  • The model deals with the database operations like creating, inserting, updating and deleting.
  • The view deals with the user interface. I mean whatever user can see on the webpage.
  • The controller is given the work of controlling the operations.
  • I mean that the MVC architecture is mainly used to separate the back end and the front end of the application i.e. presentation and logic to make it more flexible and friendly.
  • And the controller works as the interface between the back end and the front end.
  • All the logic of retrieving data from the database, displaying it to the user on webpage and again updating the data received from the user back to the database is done in controllers.
  • Only the controller decides what to display on the webpage and how to display it.
  • Also controller is a very important core part of your application since it determines the http request to run your application.
  • You can omit view and model from your application, but not controller.
  • Let us see the uses of controller:
  1. Deciding URI of the application:
    • The URI of any application in the MVC architecture is shown below:
    • http://domain_name/[controller-class]/[controller-method]/[arguments]

    • In the above web address the domain_name can be the localhost or your server address, but further we have the controller-class, controller-method and arguments. So here controller-class and controller-method decides which application and which method in that application to display.
    • Actually all the logic of what to display is written in the controller class. So we can say that controller is a class file which decides the content to be displayed as well as the URI of the application.
    • Let us try an example by creating a file greet.php in the xampp/htdocs/CodeIgniter/application/controllers folder on our machine and write the following code in it:
    • <?php
      	class Greet extends CI_Controller
      	{
      		public function index()
      		{
      			echo "<br><h1>HAPPY NEW YEAR 2015!!</h1>";
      		}
      	}
      ?>
      
    • In the above code we have defined a class named Greet which extends CI_Controller class so that it can inherit all the variables and methods declared in CI_Controller class.
    • Name of our file is greet.php and the name of controller class in it is Greet. You can have any name to the file but it is convenient to remember in this way.
    • And the naming convention is that the class name should always start with an initial capital letter.
    • We have a function named index() in our class. If the function is named index, it is automatically called even if it is not called in the URI. I mean even if we don’t specify its name in the URI after the controller-class name, it is called automatically by default.
    • We have a message of happy new year in it.
    • To view output of the above code, open your browser and write the following address in its address bar.
    • http://localhost/CodeIgniter/index.php/greet

    • Here, localhost is your localhost server, CodeIgniter is the CodeIgniter framework folder which is in your local server, index.php is the PHP page provided in CodeIgniter by default and greet is the greet.php file of your Greet controller-class.
    • Remember here we have not specified the controller-method in the URI.
    • The output is shown below:
    • Greet_controller_output_1
      fig 1

    • Since index is called by default, you can view the output even though it is not specified. But if there is another function with other name in your controller class and you want to call it then? Let us demonstrate it.
    • Add one more function with name republic to the existing Greet class. The complete Greet class is shown below:
    • <?php
      	class Greet extends CI_Controller
      	{
      		public function index()
      		{
      			echo "<br><h1>HAPPY NEW YEAR 2015!!</h1>";
      		}
      		
      		public function republic()
      		{
      			echo "<br><h1>HAPPY REPUBLIC DAY</h1><br>";
      		}
      	}
      ?>
      
    • We have added republic() function to Greet class.
    • Now if we specify the URI without the method name as shown below:
      http://localhost/CodeIgniter/index.php/greet
      we will get to see the output of index() function. i.e. HAPPY NEW YEAR 2015!!
    • But to see the output of republic() function we need to specify the controller method name also as shown below:
    • http://localhost/CodeIgniter/index.php/greet/republic

    • Here we have the controller-class greet as well as the controller-method republic specified in the URI. Output obtained from this URI is shown below:
    • Greet_controller_output_2
      fig 2

    • Thus a controller decides the URI of the application.
  2. Passing URI segments as parameters to the controller function:
    • You can have parameters passed into the controller’s function with the use of URI.
    • This can be achieved if the URI of the application contains more than 2 segments.
    • For example the CodeIgniter, controller-class, controller-method and the other arguments following it are called the segments of the URI i.e. the URI is formed of all these path segments.
    • So if we pass more values following the controller-method in the URI, we can use them as parameters to the specified function (method) of the controller.
    • In other words we can pass values to the function while running the page.
    • For example, let us modify our republic() function in the Greet controller-class to accept parameters from the URI.
    • The complete Greet class with modified republic() function is given below:
    • <?php
      	class Greet extends CI_Controller
      	{
      		public function index()
      		{
      			echo "<br><h1>HAPPY NEW YEAR 2015!!</h1>";
      		}
      		
      		public function republic($day,$mon,$imp)
      		{
      			echo "<br><h2><i>".$day." ".$mon."</i> is the <i>".$imp."</i> DAY</h2>";
      			echo "<br><h1>HAPPY ".$imp." DAY</h1><br>";
      			
      		}
      	}
      ?>
      
    • Here we have passed 3 parameters to the function republic(). The parameters are $day representing the date, $mon representing the month and $imp representing the speciality of the day.
    • These parameters are used in the statements using the concatenation operator period (.) in the function. I have just displayed them but we can pass any values and perform any manipulation we want with them.
    • Now to see the output we have to specify the values in the URI. So write the following address in the address bar of the browser:
    • http://localhost/CodeIgniter/index.php/greet/republic/26/January/REPUBLIC

    • In the above address greet is the controller-class, republic is the controller-method and the values 26, January and REPUBLIC following the controller-method are the values passed for the parameters in republic() function.
    • The output is shown below:
    • parameters_passed_to_republic_method_output
      fig 3

    • Now if I pass parameters as 15, August and INDEPENDENCE, it will give the output as follows:
    • parameters_passed_to_republic_method_output_1
      fig 4

    • Thus a URI can pass parameters to a controller method.
  3. Defining a Default Controller:
    • Default controller means the controller which runs first automatically when the root URL is provided.
    • Until this point we have specified all the segments in URI to call the controller. But if it so happens that we just specify the root URL i.e. http://localhost/CodeIgniter and the default method i.e. index() method runs and gives us the output.
    • This can happen by doing some settings in codeigniter.
    • You need to open the CodeIgniter/application/config/routes.php file.
    • Open the routes.php file with notepad++.This file contains a variable default_controller on line number 41 as shown below:
    • $route['default_controller'] = "welcome";
    • This has the page welcome set as the default controller. This is the page which we had seen after installing and running codeigniter.
    • Now to make our application controller as the default controller we will have to replace welcome with our controller-class file name as shown below:
    • $route['default_controller'] = "greet";
    • The routes.php file with the changed default_controller is shown below:
    • routes_php_file_with_default_controller_set
      fig 5

    • Now save the file and close it. To view its output now instead of writing the URI with controller-class and controller method we can just call it using the root path as shown below:
    • http://localhost/CodeIgniter

    • The output is shown below:
    • default_controller
      fig 6

    • In the above task we called index() method of the controller by default. But if you want to call the republic() method as the default method in the default controller, you will only have to modify the default_controller variable in routes.php file as shown below:
    • $route['default_controller'] = "greet/republic";
    • Here, greet is the controller-class file name and republic is the method which is to be called by default.
    • But we have parameters in our republic() method. So I am giving default values to the parameters as shown below:
    • public function republic($day=26,$mon='January',$imp='REPUBLIC')
      		{
      			echo "<br><h2><i>".$day." ".$mon."</i> is the <i>".$imp."</i> DAY</h2>";
      			echo "<br><h1>HAPPY ".$imp." DAY</h1><br>";
      			
      		}
      
    • I have given values 26, January and REPUBLIC to the parameters $day, $mon and $imp respectively as shown in the code.
    • We provided default values to parameters in republic() function, only because it is a parameterized function and we want to call it by default using just the root URL. If there were no parameters in republic() method, changing the default_controller variable in routes.php file was enough to run it by default.
    • After this change, you will now get the republic() method as the default controller method by putting the following address in the browser’s address bar:
    • http://localhost/CodeIgniter

    • The output is shown below:
    • default_controller_with_other_method
      fig 7

    • Thus we can make our application to work as default application by setting our controller-class file as the default one.

Thus we studied 3 uses of controllers in codeigniter in this Controllers and their uses in CodeIgniter PART-1 tutorial. We will see rest of the uses in next part.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -