How to Use Composer With Codeigniter

Composer: Composer is an application-level dependency manager for the PHP programming language that provides a standard format for managing dependencies of PHP software and required libraries. It was developed by Nils Adermann and Jordi Boggiano, who continue to manage the project.

okay, how to use it with CI, here is the simple one-line solution – we need include autoload.php file in CI’s bootstrap file index.php or you can place any were in the app but autoload it.

i assume that you already have installed composer on your machine.If not please visit : https://getcomposer.org/

1. Download and install Codeigniter, Create a composer.json file in your project root and define dependencies.
Ex:

2. Now run composer, it will download all the dependencies in vendors directory in your project root.To install dependencies, open terminal(command prompt) then go to your project root.(Ex: cd c:\xampp\htdocs\).
now run this.

php composer.phar install
If you did a global install and do not have the phar in that directory run this instead:
php composer install

Then you should notice composer creating a ./vendors folder in your application root.

3.Add autoload.php to your Codeigniter’s index.php before require_once BASEPATH.’core/CodeIgniter.php’;

That’s it.

How to use loaded Class or libraries

Here is my example- welcome controller, index method i am using mpdf with is installed by Composer.

That’s it.

we can even make it elegant because the above method may be not very convenient to switch to other components in future,unit testing also very difficult ..etc . It would be better if some class with its interface to be placed between the component and your application.so make a CodeIgniter library that extends the installed component.

create a file within application/libraries/ and name it Pdf.php with the following content:

How to use it

Here is the simple example.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.