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); } } ?>
- 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.
$autoload['libraries'] = array();
$autoload['libraries'] = array('calc');
<?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>"; } } ?>
$arith=new Calc();
http://localhost/CodeIgniter/index.php/arith_operation
Thus we learned practically how to auto-load a file/code in CodeIgniter in this Auto-loading in CodeIgniter tutorial.