![]() |
How to create custom library in CodeIgniter? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: External Resources (https://forum.codeigniter.com/forumdisplay.php?fid=7) +--- Forum: Learn More (https://forum.codeigniter.com/forumdisplay.php?fid=15) +--- Thread: How to create custom library in CodeIgniter? (/showthread.php?tid=66662) |
How to create custom library in CodeIgniter? - ci3school - 11-19-2016 CodeIgniter libraries are collection of classes located in libraries folder(i.e, application/libraries and system/libraries). Libraries are used to make the collection of related methods. You can make as many libraries in CodeIgniter as per your convenience. Let’s start the tutorial for create custom library in CodeIgniter. Visit here:- ... OP will provide real link, please ... RE: How to create custom library in CodeIgniter? - meow - 10-08-2017 Considering that CI4 will be installed via composer: you can create your libraries without worrying about codeigniter methods and then upload your library to packagist. From within your codeigniter directory, composer require "companyName/yourLibrary" and then voila. Your library file may start off like PHP Code: <?php then in your controller file, you can do PHP Code: public function index() { RE: How to create custom library in CodeIgniter? - invokker - 04-08-2021 (11-19-2016, 01:34 AM)ci3school Wrote: CodeIgniter libraries are collection of classes located in libraries folder(i.e, application/libraries and system/libraries). Libraries are used to make the collection of related methods. You can make as many libraries in CodeIgniter as per your convenience. Steps to follow while creating a custom library in CodeIgniter #1 Open Library folder Assuming that you are aware of the directory structure of CodeIgniter. To create your own library, open application/libraries folder. All the libraries defined by you will be residing in this library itself. #2 Create a library file In application/libraries folder, create a PHP file with name myLibrary.php. Now, to successfully create a library, you need to create a library class too. #3 Create Library Class Open the myLibrary.php file and create a class with the same name as file,i.e. myLibrary class. The code will be like I am mentioning below. Doing this will create the library. However, it is useless until you put some code in it. <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Mylibrary { } #4 Put some code in your library Let’s assume that this library will be used to calculate mathematical operations. So I am going to add a few methods to do addition and subtraction. Well, this is the kind of kids method, but my purpose is to explain the working. So the first method will be plus() and the other one will be minus(). Both functions take two parameters to perform operations. <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Mylibrary { function plus($num1, $num2) { $sum = $num1 + $num2; return "<h2>Sum of $num1 and $num2 is: $sum</h2>"; } function minus($large, $small) { return "<h2>Result of $large - $small is: " . ($large - $small) . "</h2>"; } } #5 Load Custom Library in CodeIgniter You can load a system library or a custom library in two ways. Either by autoload or by loading them in controller wherever required. Whenever we load any library, firstly, it is searched in the system/libraries folder. If it is not found in that folder, it will then be searched in application/libraries folder. |