Change language

Libraries in Codeignitor

Download the library to use it. Create a controller users.php,and then use the below code to send an email.Controller: users.php defined ( ’BASEPATH’ ) OR exit ( ’No direct script access allowed’ ); class Users extends CI_Controller { public function index() { // Download mail library $this -> load-> library ( ’email’ ); // Use built-in methods to send email $this -> email-> from ( ’abc @’ , ’your name’ ); $this -> email -> to ( ’xyz @’ ); $this -> email -> subject ( ’Test’ ); $this -> email -> message ( ’Welcome to GeeksforGeeks’ ); $this -> email -> send(); } }
If we need to load multiple libraries, we can create an array and then define all the library names in that array.
$this-> load-> library (array (’form_validation’,’ email’));
Custom libraries:we have seen that there are already many built-in libraries in CI. If we need some functionality that is not in the CI library, then we can create our own library. Let’s see how to create a custom library in CI. 
Built-in libraries are available in the system folder, but custom libraries must be created in the application / library folder. We must remember that the file name must be the same as the class name, and the first letter of the file name and class name must be in upper case. Create a Test.phpfile in the application / library folder. Below is an example of creating functionality in our custom library.Custom library: Test.php < ? php class Test { public function abc() { echo "Welcome to GeeksforGeeks" ; } }
Custom libraries can be used just like the built-in libraries in controller. So in our users.php controller, use 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 library $this -> load-> library ( ’test’ ); // Using a custom library function $this -> test-> abc(); } }
Welcome to GeeksforGeeks
Extending Libraries:In this section we will see how to extend the built-in CI libraries. It is also created in the same application / library folder as we did for the custom libraries. Now we need to remember that we need to add the MY_, prefix when we name our library file, and also use the extendskeyword to inherit all the properties of the email library. So create a MY_Email.phpfile in your application / libraries folder and use the code below.Extended Library: MY_Email.php class MY_Email extends CI_Email { // Add function to email library public function test() { echo "This is test function for extended Email library " ; } }
Once the email library is expanded, we can use its additional the "test" function in our controller. So in your users.php controller, use the code below to call the test function from the Email library.Controller: users.php defined ( ’BASEPATH ’ ) OR exit ( ’ No direct script access allowed’ ); class Users extends CI_Controller { public function index() { // Load the library $this -> load-> library ( ’email’ ); // Call the function $this -> email-> test(); } }
This is test function for extended Email library
So far expanding our libraries and adding our own functionality, now if we want to completely override the built-in libraries function and don’t want to use any function from the built-in library, we can do it. All we have to do is create a file in the application / library named Email.php (or whatever library needs to be overridden) and then write our own function. We don’t need to use the extends keyword here. Below is the code & # 8230;Overridden Library Name: Email.php

class CI_Email { public function test() { echo "This is test function for overridden Email library " ; } } ?> Loading this library and calling the function will be the same as before what we did in the users.php controller. 
Exit :
This is test function for overridden Email library
get_instance():Codeignitor creates a superobject, and then subobjects are created. We use the $this keyword to access the object. We cannot use this keyword except for controllers, models and views. So if we need to load another built-in helper or library into our custom library, $this won’t work because those custom libraries are outside the scope of the superobject. To solve this problem, we need to use the get_instance() method in our custom library. First, we need to get an instance of the superobject in a variable. Below is the line for our custom library to get the original reference to the codeignitor object.
$CI = & amp; get_instance();
We should notice that we get the link or address of the original codeignitor object using "& amp;" instead of making a copy. After that we can load any helper, library or model. Below is the complete code for the custom library Test.php Custom library: Test.php class Test { public function abc() { // Assign the CodeIgniter object to a variable $CI = & amp; get_instance(); $CI -> load -> helper ( ’array’ ); } }


Best laptop for Sims 4


Best laptop for Zoom


Best laptop for Minecraft


Best laptop for engineering student


Best laptop for development


Best laptop for Cricut Maker


Best laptop for hacking


Best laptop for Machine Learning


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers


Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method