Tutorials

Article Content:codeigniter composer integration

codeigniter composer integration

codeigniter composer integration.

Many people so confused about integrate composer with codeigniter, also the documentation is not clear about using it, in the last day I saw a tutorial that used a very bad way to integrate composer with codeigniter 3.0, after this, and by luck I saw a question in stackoverflow with confusing answers that not clearly guide how to use composer with codeigniter, which make me thought to create a tutorial on this subject with step by step example.

Not just that, I though to make it very easy by doing a video for my friends to help them doing it faster with real world example, the example about pdfparser library which is a standalone PHP library, provides various tools to extract data from a PDF file .
So by the end of this tutorial you will have the freedom to use any package or library that compatible with composer in codeigniter, let is do it.

Step One
 Go to Composer website , download and Install Composer-Setup.exe on your computer, in this example I use windows OS because it is my current system but you will find an easy instructions for Linux or mac in composer page by using Command-line installation

Step Two
Open application/config/config.php, find the setting $config[‘composer_autoload’]. Set it to TRUE. Or copy and paste it manually $config[‘composer_autoload’] = TRUE;

Step Three
Create new json file in this path application/composer.json

Step Four
Go to packagist.org and search for the package you want ,in our example it will be kreatys/pdf-parser-bundle
, then copy the require text below and add it to composer.json as a json content, like this 

{
    "require": {
        "smalot/pdfparser": "*"
    }
}

Step Five
Open the terminal and with cd commands go to the folder application/, in my example I went to C:\xampp\htdocs\codeigniter_demo\application, then Type the command composer install (or php composer.phar install),and wait until download finish.

Step Six
Finally to use this package go to your controller function, include autload.php file from vendor path, and use it like documentation,go to the function in the browser and you will see the parser pdf text

// Include Composer autoloader if not already done.
include_once APPPATH . 'vendor/autoload.php';

// Parse pdf file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile(APPPATH.'vendor\smalot\pdfparser\samples\Document1_foxitreader.pdf');

$text = $pdf->getText();
echo $text;
Important Note :
Don't include any pacadge to index.php because it is a dirty way,you can instead create a custom library

Conclusion 

Using composer is very essential task for any php developer because it is a framework agnostics, so if you a codeigniter developer, Yii developer, laravel developer or just have a custom code you will benefit from composer because it will give you the freedom to use others code easily without worrying about what library depend on to work or what version you need to work with.



Leave a Reply

Your email address will not be published.


Notify me of followup comments via e-mail.
You can also Subscribe without commenting.