How to use the CI libraries and helper?
Syntax#
-
$this->load->library(‘library_name’);
-
$this->library_name->function_name();
-
$this->load->library(‘cart’); # for helper $this->load->helper(‘helperName’);
-
$this->cart->insert($Array);
Creating and calling a library
In order to use libraries in CodeIgniter, you need to create a library.
class Pro {
function show_hello_world()
{
return 'Hello World';
}
}
In this library, which is called is pro.php, this file must be added to the following path.
Path: \xampp\htdocs\project\application\libraries
Now you can use it in your controller. Code to load this library in the controller:
$this->load->library('pro');
Code to use the library functions:
class Admin extends CI_Controller {
function index()
{
$this->load->library('pro');
echo $this->pro->show_hello_world();
}
}