Helpers in Codeignitor

PHP

Controller: users.php defined ( 'BASEPATH' ) OR exit ( 'No direct script access allowed' ); class Users extends CI_Controller { public function index() { // Load Form Assistant $this -> load-> helper ( 'form' ); } }
?>
If we need to load multiple helpers, we can create an array, and then define all the helper names in that array.
$this-> load-> helper (array ('form',' email', 'url'));
Custom Helpers:Codeignitor already has a lot of built-in helpers, but if we need to create a function that is not in a helper, then we can create our own custom helper and use it just like the built-in helpers. Built-in helpers are available in the system folder, but a custom helper must be created in the application / helpers folder. Create a file abc_helper.phpin the application / helpersfolder. Below is an example of creating functionality in our custom helper.Custom helper: abc_helper.php < ? php function test() { echo "Custom helper for codeignitor " ; } ?> Custom helpers can be used in exactly the same way as built-in helpers in controller. So in our controller users.phpuse the code below to test this.Controller: users.php

defined ( 'BASEPATH' ) OR exit ( ' No direct script access allowed' ); class Users extends CI_Controller { public function index() { // Load Custom Assistant $this -> load-> helper ( 'abc' ); test(); } } ? Output:
Custom helper for codeignitor
Helper extension:if we want to add some other function to the already built-in helpers, we can do it easily. Extending a helper means adding additional functionality to built-in helpers or overriding built-in functions. It is also created in the same application / helpers folder as we did for custom helpers. Now we need to remember that we need to add the prefix ' MY _ & # 39 ;, when we name our helper file. So create a MY_array_helper.phpfile in the application / helpersfolder and use the code below.Advanced Helper: MY_array_helper.php // Add the function to the inline helper array function test() { echo "Extend array helper in codeignitor" ; } ?> After the sub array is expanded we can use it additional function ' test ' in our controller. So in the users.php controller, use the code below to call the test function from the helper array, and then we can use our own function.Controller: users.php defined ( ' BASEPATH' ) OR exit ( 'No direct script access allowed' ); class Users extends CI_Controller { public function index() { $this -> load-> helper ( 'array' ); // Call the function test(); } } ?> Output:
Extend array helper in codeignitor
So far we have extended our helpers and added our own functionality, now if we want to override the built-in helper function, we can do that too do. To override a function from a built-in helper, we need to give the same function name that already exists in that helper. So, create a MY_array_helper.phpfile in the application / helpersfolder and create the ' element ' function that already exists in system / helpers / array_helper.php.Advanced Helper: MY_array_helper.php // Override the helper array element function. function element() { echo "Overridden extended array helper in codeignitor" ; } ?> Now in our controller users.php if we load the helper over the array and call the function ' element ', it will give the result of our overridden array helper function instead of the function ' element ' from system / helpers / array_helper.php. It will completely override the functionality of the array helper and give the new result that we defined in ' MY_array_helper.php & # 39 ;.Controller: users.php defined ( 'BASEPATH' ) OR exit ( 'No direct script access allowed' ); class Users extends CI_Controller { public function index() { $this -> load-> helper ( 'array' ); // Define an array for the element function $arr = [ 'abc' = > ' ABC' , 'xyz' = > 'XYZ' ]; // Call the element function, but it won't // give the result of the element function from // a built-in helper array exists in the system folder echo element ( 'abc' , $arr , 'Not Found' ); } }
?>
Output:
Overridden extended array helper in codeignitor
So it is clear from the above example that as soon as we override the inline helper array function, it gives us the result of our own defined function ' element ' in MY_array_helper.php instead of the old function ' element ' in the array helper. If we delete our MY_array_helper.php file, we get the result of the ' element ' function that exists in system / helpers / array_helper.php
Exit:
 ABC




Get Solution for free from DataCamp guru