Welcome Guest, Not a member yet? Register   Sign In
How to create custom library in CodeIgniter?
#1

(This post was last modified: 11-19-2016, 03:46 AM by ciadmin.)

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 ...
Reply
#2

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
namespace MyCompanyName;

class 
math {
 public function 
multiply($a 0$b 0) {
 
  return $a $b;
 }



then in your controller file, you can do

PHP Code:
public function index() {
 
$object = new MyCompanyName\math;
 
$answer $object->multiply(45);

 
// code here to pass the variable $answer to the view

Reply
#3

(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.
Let’s start the tutorial for create custom library in CodeIgniter. Visit here:- ... OP will provide real link, please ...

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.
Today Is Name, Tomorrow Is Legend! 

Best CRM Software in India
Reply




Theme © iAndrew 2016 - Forum software by © MyBB