Composer is needed if we work with many libraries .
Composer will help us install, download, update, and finddependencies from the library used.
In CodeIgniter, composer has started to be supported in version 3. However, the autoload has not been activated by default .
On this occasion, I will discuss how to configure CodeIgniter to use Composer.
Let's start…
CodeIgniter configuration
Open the CodeIgniter configuration file at
application/config/config.php
. Then fill the autoload value for the Composer as follows:$config['composer_autoload'] = "vendor/autoload.php";
Library Installation
Now try opening the file
composer.json
in the project's rootdirectory. Then add the FPDF library.{
"description": "The CodeIgniter framework",
"name": "codeigniter/framework",
"type": "project",
"homepage": "http://codeigniter.com",
"license": "MIT",
"support": {
"forum": "http://forum.codeigniter.com/",
"wiki": "https://github.com/bcit-ci/CodeIgniter/wiki",
"irc": "irc://irc.freenode.net/codeigniter",
"source": "https://github.com/bcit-ci/CodeIgniter"
},
"require": {
"php": ">=5.2.4",
"setasign/fpdf": "1.8.1"
},
"require-dev": {
"mikey179/vfsStream": "1.1.*"
}
}
This is just an example.
You may add or use libraries other than FPDF.
After that, type the following command to install all the libraries:
composer update
Wait until the process is complete.
Library Experiments
Now try adding the following code to the Controller :
<?php defined('BASEPATH') OR exit('No direct script access allowed');
// Class untuk membuat laporan
class Laporan extends CI_Controller{
function __construct(){
parent::__construct();
}
/**
Fungsi untuk menampilkan menu laporan penerima bantuan
@param none
@return void
**/
function penerima_bantuan(){
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
}
}
After that, try to see the results.
We use FPDF Library successfully on CodeIgniter.
The above experiment is actually to make sure the library from the Composer is loaded or not.
The rest you can experiment by creating a Class extend from the library .
0 Komentar untuk "How to use the Composer on CodeIgniter"
Silahkan berkomentar sesuai artikel