Web Programming TutorialsAuto-loading in CodeIgniter

Auto-loading in CodeIgniter

Today we will see a very interesting and important topic of loading important files automatically in this Auto-loading in CodeIgniter tutorial.

In our application we need some files that can be accessed anywhere we need them. We know that we can include such files in the page where we need it using include or require statement in PHP. But if we get such technique which will load all the required functions and classes without everytime linking them by using include or require, then it will be great news. This great news is given to us by our CodeIgniter.

  • CodeIgniter provides us with an auto-load feature which permits us to load libraries, models, helpers etc automatically when the system runs.
  • So if we want something to be available globally in our application, it should be auto-loaded.
  • Let us see what can be auto-loaded:
    • Classes in the libraries folder of CodeIgniter.
    • Models in the models folder.
    • Helper files in the helpers folder.
    • Custom config files in the config folder.
    • Language files in the system/language folder.
    • Packages.
  • All the above things can be auto-loaded by adding the file name in which the class/code to be auto-loaded resides, to the array in the file named autoload.php which is located in application/config folder.
  • All the instructions to auto-load the particular file are provided in the autoload.php file.
  • It contains separate arrays to load libraries, models, helpers, languages etc.
  • Once just have a look to your CodeIgniter/application/config/autoload.php file and go through it.
  • Let us demonstrate an example to load a library file automatically using the auto-load feature.
  • For this we will require a library file, so let us create it.
  • Open a notepad++ document and save it as calc.php in the CodeIgniter/application/libraries folder. This will be our library file.
  • Now write the following code in calc.php: We are going to perform simple arithmetic operations in this calc.php file for understanding, you can write code for any task you want:
  • <?php
    	class Calc
    	{
    	   function add($n=0,$m=0)
    	   {
    		return ($n+$m);
    	   }
    		
    	   function sub($n=0,$m=0)
    	   {
    		if($n>$m)
    			return ($n-$m);
    		else
    			return ($m-$n);
    	   }
    		
    	   function mul($n=0,$m=0)
    	   {
    		return($n*$m);
    	   }
    		
    	   function div($n=0,$m=0)
    	   {
    		if($n>$m)
    			return ($n/$m);
    		else
    			return ($m/$n);
    	   }
    		
    	   function rem($n=0,$m=0)
    	   {
    		return ($n%$m);
    	   }
    	}
    ?> 
    
  • In the above code we have defined a class Calc. According to the naming conventions the initial letter of the class name is capital (Calc) and we have even named the file with same name with small letters (calc.php). This is just for convenience; otherwise we can give any name.
  • In the Calc class we have defined functions add(), sub(), mul(), div() and rem() with 2 parameters each.
  • Each parameter is provided with default value 0.
  • As the name specifies these functions perform addition, subtraction, multiplication, division and modulus operations respectively and return the calculated values.
  • The calc.php file in application/libraries folder is shown in the figure below:
  • calc_php_file_in_library
    fig 1

  • Our library file is ready. Now we want to auto-load it so that we can use it in our application.
  • Here is the simple process to be followed for the same:
    • Open the application/config/autoload.php file.
    • Go to the array of the section where you want to add your file. I mean if you want to add a library file i.e. a file in the library folder of application folder, you need to go to the array of library section in the autoload.php file. If you want to add a model, then go the array of model section and so on.
    • Next add the file name of your class file into the particular array. The added file name should not contain the extension. I mean if your file name is app.php, just specify app in the array excluding the .php extension.
  • Let us follow the auto-loading procedure for our calc.php library file.
  • In fig1 we can see that our calc.php file is in application/libraries folder. So we will add it to the array for libraries in autload.php file.
  • When we open the application/config/autoload.php file, we will go to the section with heading Auto-load Libraries.
  • There we can see an empty $autoload[‘libraries’] array as shown below:
  • $autoload['libraries'] = array();
  • We will just add our calc.php file name without its extension to this array as shown below:
  • $autoload['libraries'] = array('calc');
  • The autoload.php file with the above statement is shown below:
  • autoload_php_file
    fig 2

  • Now we finished auto-loading the calc.php file. So let us check it by using it.
  • Open a notepad++ document and save it as arith_operation.php in the CodeIgniter/application/controllers folder.
  • Write the following controller class in this arith_operation.php file:
  • <?php
    	class Arith_operation extends CI_Controller
    	{
    	   public function __construct()
    	   {
    		parent::__construct();
    	   }
    		
    	   public function index()
    	   {
    		$a=100;
    		$b=10;
    		$arith=new Calc();
    		echo "<br><hr>";
    		echo "Addition: $a + $b = ".$arith->add($a,$b);
    		echo "<br>Subtraction: $a - $b =  ".$arith->sub($a,$b);
    		echo "<br>Multiplication: $a * $b =  ".$arith->mul($a,$b);
    		echo "<br>Division: $a / $b = ".$arith->div($a,$b);
    		echo "<br>Modulus: $a % $b = ".$arith->rem($a,$b);
    		echo "<br><hr>";
    	   }
    	}
    ?>
  • Here we have an Arith_operation class which extends a CI_Controller class.
  • A constructor is written for the class and we have an index() function which uses the auto-loaded calc library class.
  • In the index function we have provided values 100 and 10 to variables $a and $b respectively.
  • Then we have made an object of Calc class, $arith using the statement given below:
  • $arith=new Calc();
  • Using this $arith object we can call the methods defined in Calc class.
  • We have called all the methods one by one as shown in the code above.
  • To see the output, write the following address in the address bar of the browser:
  • http://localhost/CodeIgniter/index.php/arith_operation

  • The arith_operation is the controller-class file name and we did not specify the controller-method name index, since it will be called automatically by default.
  • The output is shown below:
  • output
    fig 3

Thus we learned practically how to auto-load a file/code in CodeIgniter in this Auto-loading in CodeIgniter tutorial.

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 -