Bladephp: Now with 8+ codeigniter, php, and laravel project 8+ php project From $37/Month From Rs. 2499/Month

How to Create Custom Library in CodeIgniter

by
Category:codeigniter
codeigniter
How to Create Custom Library in CodeIgniter

In this tutorial i will show you how to create custom library in codeIgniter. There are many readymade classes available for the payment gateway, pdf generation, some third party API. So let’s see how to integrate these classes to codeigniter and use it like a library. We can create our own class and use it as library. In this tutorial use custom dompdf librarie. You may also like How to convert HTML to PDF in PHP and How to Create PDF using DOMPDF in Codeigniter.

Steps For How to Create Custom Library in CodeIgniter

Step 1. Connect To Library File

In this step we have go to folder application/libraries and create libraries.

Step 2. Create Library File

Create a PHP file with YourLibraryName_lib.php (Dompdf_gen.php)

Step 3. Library File Code

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dompdf_gen {
    public function __construct() {
            require_once APPPATH.'third_party/dompdf/dompdf_config.inc.php';
            $pdf = new DOMPDF();
            $CI =& get_instance();
            $CI->dompdf = $pdf;
    }
}

Step 4. Use Library In Controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Orderhistory extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->library('dompdf_gen');
    }

    public function index()
    {
        $html = 'Welcome';
        $this->dompdf->load_html($html);
        $this->dompdf->render();
        $output = $this->dompdf->output();
        file_put_contents('assets/pdf_invoice/pdf.pdf', $output);
    }
}
?>

And if you like this tutorials please share it with your friends via Email or Social Media.


1 Comments

MAHENDRA MANJHI

15 May 2018

Very nice to develope it but just want to create new file or website

Html code work in comment box


Related Post