
For all web app developers who have started using modern frameworks like Symfony, Silex, Laravel, etc. and have wished for composer support inside their CodeIgniter application, I will try to show you the most efficient and standardized way to achieve this goal. I have also added this feature to my CodeigniterPlus project, and if you are familiar with that project, you can also check it out. If you are concerned about getting support for Composer with CodeIgniter in an existing application, you can do that without worry.
Why You Should Consider Composer Support?
Composer is the ultimate solution for PHP developers to manage application dependencies easily and efficiently. Here, with dependencies, I mean various third-party open-source libraries/plugins, PSR standards, etc.
Currently, to add a third-party library, you will have to download it, add the library source to ‘application/third_party
‘ directory, create a mapper library(if needed) inside ‘application/libraries
‘ directories and load it manually(or automatically via autoload.php). What if all of this was done in a very simple way?
Also, as for now, you will have to add all library codes into your source code repository and make the repo size unnecessary. On the other hand, with the help of the composer, you can ignore that part and only add a dependency list on the repo, which in turn can be loaded on the server dynamically without affecting the repository size.
Though you should easily follow this tutorial with the least knowledge of Composer, it will be better and easier for you, if you are familiar with composer.
Get Your Composer Platform Ready:
If you haven’t yet, install Composer on your system first. You can get detailed instructions on official composer getting started documentation. After that, on your project root directory, add a file named ‘composer.json’ and add at least one of the dependencies (I used the Monolog library as an example) you want to install:
{
"require": {
"monolog/monolog": "*"
},
"minimum-stability": "dev"
}
Code language: JSON / JSON with Comments (json)
Then, in that directory, run the ‘composer update’ command on your command-line tool. If everything goes OK, you should notice a vendor directory and composer. Lock files are created there. Congratulations—you successfully finished this part!
Read The Complete CodeIgniter Tutorials Series By CodeSamplez.com
Dirty/Easy Way To Integrate Composer With Codeigniter:
The easiest and dirtiest way to add all dependencies is to add a line like the following on your CodeIgniter’s index.php file:
include "./vendor/autoload.php";
Code language: PHP (php)
And you should be done!
It’s super easy, isn’t it? Now, you can start defining and using all classes inside the vendor directory. Composer handles all the dependencies by dynamically creating a single ‘autoload.php’ that loads all those libraries.
The Alternative Recommended Way:
Though the previous way was easy and simple enough, I wouldn’t say I like such hacks. As an alternative better way, you can create a simple class that, in turn, loads the required autoload.php file and loads it with the help of CodeIgniter’s “application/config/autoload.php” file.
The custom class inside.’application/libraries
‘ directory:
<?php
/**
* Description of MY_Composer
*
* @author Rana
*/
class MY_Composer
{
function __construct()
{
include("./vendor/autoload.php");
}
}
Code language: PHP (php)
Now simply add this class in the auto-load array as the very first element:
$autoload['libraries'] = array('MY_Composer','database','session');
Code language: PHP (php)
And here we are, done as well. Great!
Final Words:
If you are already in touch with official Codeigniter site news, you might be aware that CodeIgniter’s updates/releases are very slow, and there is the possibility of its death as well( though I appreciate the development of the Codeigniter community, which is still active but has no stable release yet). In addition, PHP has already been updated with many features of modern web application development.
So, to get a taste of the modern era of PHP development, such a hack like this could be very much helpful to developers who are working/maintaining CodeIgniter applications developed years earlier(Moreover, Codeigniter can still be considered as a least learning curve framework also). Happy coding 🙂
Discover more from CodeSamplez.com
Subscribe to get the latest posts sent to your email.
I have added it in a helper file and added this helper to CI autoload config file
I liked your advice on not putting the include in the index.php. But I decided to put the include in a pre_controller hook as I felt making a library file to just to include another file didn’t make semantic sense to me.
Thanks for simple and nice tutorial.
I think that you forgot this line after “<?php"
==
defined('BASEPATH') OR exit('No direct script access allowed');
==
in 'MY_Composer' lib
In CI 3.0 there is a config line
$config[‘composer_autoload’] = true;
One more reason to move to this release
I already do what you write but still have error “Message: Class ‘Lavacharts’ not found”.when I do:
$lava = new Lavacharts;.
Please help. Thanks in advance
Seems like you are not using proper namespacing. It suppose to be ‘new Khill\Lavacharts()’ for the library you mentioned.
Thank you for your help… I got the syntax from http://lavacharts.com/ And I already found my problem. I didn’t write ‘use \Khill\Lavacharts\Lavacharts;’ . But anyway your tutorial is very helpful. Thanks again 🙂
I put the code in MY_Controller.php and extends CI_Controller, it also works good. Thanks for your article.
Thank you mate, have been stumbling for the correct way to include libraries and have been doing it by the dirty way 😛