CodeIgniter Helper Function are available any where in the code, more then 20 CI Helpers are there(URL, Form, Directory, …etc). You can Easily Extend CodeIgniter Helpers. CodeIgniter Helper is just a php File with bunch of functions , CI helper is not a class.
You can extend a CodeIgniter helper in a similar way to controllers and models. First save a file called “MY_[name]_helper.php” to application/helpers where [name] is the name of the helper. To extend the url helper it would be “MY_url_helper.php”. There is no need to extend any class here as helpers are just a bunch of functions. Just open php tags and start writing your functions to this file. Remember not to add a closing php tag as it may cause trouble with headers. It’s as simple as:
You load an extended helper the same way you load a normal helper. To load the url helper from a controller (which is the best practice) just use $this->load->helper(‘url’);. This makes your new functions available globally. Remember this means you access them directly (not with $this->url_helper->my_function()).
If you use this helper in a lot of locations you can have it load automatically by adding it to the autoload configuration file.
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
You can extend a CodeIgniter helper in a similar way to controllers and models. First save a file called “MY_[name]_helper.php” to application/helpers where [name] is the name of the helper. To extend the url helper it would be “MY_url_helper.php”. There is no need to extend any class here as helpers are just a bunch of functions. Just open php tags and start writing your functions to this file. Remember not to add a closing php tag as it may cause trouble with headers. It’s as simple as:
<?php
// save in applications/helpers/MY_url_helper.php
function force_ssl() {
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") {
$url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
redirect($url);
exit;
}
}
?>
You load an extended helper the same way you load a normal helper. To load the url helper from a controller (which is the best practice) just use $this->load->helper(‘url’);. This makes your new functions available globally. Remember this means you access them directly (not with $this->url_helper->my_function()).
If you use this helper in a lot of locations you can have it load automatically by adding it to the autoload configuration file.
$autoload['helper'] = array('url');
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
Comments