Controllers and their use in CodeIgniter PART-2

0
3019

We saw some of the controller uses in last tutorial, today we will learn rest of them in this Controllers and their use in CodeIgniter PART-2 tutorial.

  1. Remapping function calls:
    • Remapping function calls is done using _remap() function.
    • Let us first understand what is remapping.
    • Our URI contains the controller-class as the first segment after the root path followed by controller-method as the second segment which is used to call the specified method of the controller.
    • So in short it is decided in URI as to which function to call. This behavior is overridden by the _remap() function in codeigniter.
    • We can provide our own routing rules using the _remap() function.
    • If _remap() function is defined in the controller class, it is always called regardless of whatever controller-method is specified in the URI.
    • The controller-method passed in the URI is passed as the parameter to the _remap() function and used to decide which method should be called.
    • Let us have an example on it: In the last lesson we had created a controller-class Greet with index() and republic() methods. We will continue with the same controller.
    • Write the following _remap() method in class Greet of greet.php file which is inside your CodeIgniter/application/controllers folder.
    • the above _remap() function has one parameter $meth in it. Value for this parameter will come from the second segment of URI i.e. the controller-method specified in the URI.
    • It will be first compared to republic string, if it matches the republic() method of the controller will be called otherwise the default method index() will be called.
    • Let any method be specified in the controller, _remap() will be the first function which will always be called before all methods and then the decision to call other functions will be taken by _remap().
    • This will make things convenient for us as to allow which function to be called on which string. I mean for example, if we want to update or change the republic() function and till then we don’t want anybody to access it. At this time we can create another function informing the user about it and asking them to try later and allow access to this page instead of republic.
    • Let us practice it in our application:
    • Write a function named maintain() in the Greet class. The function is shown below:
    • Now the _remap() function is changed as follows to show maintain() instead of republic():
    • The complete modified Greet class is shown below:
    • We have just added horizontal line tags in index() and republic() functions.
    • Now write the following address in the address bar of the browser:
    • http://localhost/CodeIgniter/index.php/greet/republic

    • Here we are calling the republic method of greet controller-class.
    • Its output is given below:
    • _remap_output_of_maintain_method
      fig 1

  2. Private functions:
    • If you want some functions to be kept private, means they should not be accessed by user through URL, codeigniter has a provision for that.
    • You just have to write that function in controller with an underscore prefixed to it, it will behave as a private function of the controller.
    • Example of a private function is shown below: It is written in the Greet class.
    • This function is a private function which contains a database username and password. This should be kept private from the outside world. Hence the function dbase is prefixed with an underscore.
    • This function cannot be called using the URL. The URI is shown below:
    • http://localhost/CodeIgniter/index.php/greet/_dbase

    • Its output is shown below:
    • private_function_output
      fig 2

  3. Organizing controllers in sub-folders:
    • We can even organize our controllers in sub-folders for simplicity, if we are building a large application.
    • What you need to do is just create folders in your CodeIgniter/application/comtrollers folder and place your controller classes in them.
    • Suppose you have a controller customer located in
    • CodeIgniter/application/controllers/customer/details.php

    • Here customer is your new folder for controller for customer and details.php is the controller file in it.
    • Methods in the controller for customer can be accessed using the following URI:
    • http://localhost/CodeIgniter/index.php/customer/details/show/punam/batra

    • Here we have customer in the URI because we are accessing the controller inside it, details is the controller-class, show is the controller-method and suppose punam and batra are the parameters passed to the show method.
  4. Constructors:
    • If you want to have a constructor in your controller class, you can have it.
    • It is very convenient for you, if you have a constructor in a class. The constructors are called as soon as the class is instantiated, so you can set some default values, do some initial process etc.
    • Let us have a constructor in our Greet controller class. It is shown below:
    • I have shown only the constructor in the code above.
    • The constructor is written as function keyword then one space is left and 2 underscores are given followed by the word construct and parenthesis().
    • The controller class inherits from the CI_Controller class, hence the constructor of Greet class overrides the constructor of CI_Controller class, but to get all its facilities we need to call the constructor of CI_Controller class which is the base class or parent class of the Greet class inside the constructor of the Greet class.
    • The statement parent::__construct(); does this work of calling parent class constructor in derived class.
    • In our constructor above we have set the value of $meth parameter to ‘index’ by default.
  5. Processing output:
    • CodeIgniter provides you with an Output class which has the work of sending your data to the web browser automatically.
    • But in some cases, you want to send your finalized data to browser yourself. CodeIgniter provides you with this facility by permitting you to add a function named _output() to your controller.
    • The _output() function always receives the finalized output and it will always be called by the Output class instead of displaying the finalized data directly.
    • Example of the class is given below:

Thus we learned various uses of controller with the completion of Controllers and their use in CodeIgniter PART-2 tutorial.

LEAVE A REPLY

Please enter your comment!
Please enter your name here