Lately, everyone wants some good (if not great) piece of code from – let’s say – Packagist.org. And there are people who don’t know how to do that. So let’s try an example to see how this works with CodeIgniter 3.
First of all, let’s analyse the problem. Composer is a great tool for importing great pieces of code. Most of them can be found on packagist.org. Now, where do we want to install these pieces of code. Many would say that, being unrelated to our application, it is a good idea to keep those packages outside of application directory. But I think differently. If an application uses a package, then that package is a part of the application. I mean: what would a welder be without a welding machine? or a footballer without a ball? So I decided to put all the packages inside the application directory.
To do this, we will have to create a composer.json file inside the application directory:
{ "description" : "My great CodeIgniter application", "name" : "Avenirer/myApp", "require": { "php": ">=5.3" } }
Of course, you can change the description and name… or you can simply delete them. Why PHP version greater or equal to 5.3? Because we want to use namespaces, no?
I sure hope you have composer installed on your computer…
If you have composer installed on your computer you simply open a command prompt and navigate to the location of you application directory (example: cd C:/xampp/htdocs/application ). Once you know you are there, you simply do a composer install . If all went well, after you’ve setup your composer for CodeIgniter, you should see a vendor directory inside the application directory. If not, I am waiting for comments after the tutorial.
Now all we have to do is to make sure we are using the CodeIgniter’s autoloader.
A lot of people prefer different places for this, like the main index.php (the index.php that takes care of starting up the CI), autoloader.php, hooks.php, etc. I prefer to do this in the config.php, specifically just after the composer_autoload configuration element ($config[‘composer_autoload’] = TRUE; ). So after that line, we require_once the Composer autoloader:
require_once APPPATH.'vendor/autoload.php';
Perfect. You now have CodeIgniter with Composer.
Usage example for CodeIgniter with Composer – Simple Excel PHP
Where do we go from here? Let’s try a package from packagist.org. Someone asked on the CodeIgniter Forum, how to use Simple Excel PHP. So why not try this one? https://packagist.org/packages/faisalman/simple-excel-php
First of all we need to install the package. To do this we copy/paste the require: “faisalman/simple-excel-php”: “dev-master” into our composer.json:
{ "description" : "My great CodeIgniter application", "name" : "Avenirer/CodeIgniterTutorial", "license": "MIT", "require": { "php": ">=5.3", "faisalman/simple-excel-php": "dev-master" } }
After we save the composer.json file, we go again into our application directory from the command prompt and write:
composer update
Once we’ve done that we should have a faisalman directory inside our application/vendor directory:
-application - -vendor - - -faisalman
Now let’s test it.
For testing reasons let’s just make a test_composer() method inside our Welcome class/controller (Welcome.php):
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Welcome extends CI_Controller { public function test_composer() { } }
As you may have seen, Simple Excel PHP uses namespacing. If we use namespaces we have to declare this. I will use a piece of code that can be found in the documentation of the package:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); use SimpleExcel\SimpleExcel; class Welcome extends CI_Controller { public function test_composer() { $excel = new SimpleExcel('xml'); // instantiate new object (will automatically construct the parser & writer type as XML) $excel->writer->setData( array ( array('ID', 'Name', 'Kode' ), array('1', 'Kab. Bogor', '1' ), array('2', 'Kab. Cianjur', '1' ), array('3', 'Kab. Sukabumi', '1' ), array('4', 'Kab. Tasikmalaya', '2' ) ) ); // add some data to the writer $excel->writer->saveFile('example'); // save the file with specified name (example.xml) // and specified target (default to browser) } }
Of course, we can skip the namespace declaration, but telling you about namespaces is not the scope of this tutorial:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); use SimpleExcel\SimpleExcel; class Welcome extends CI_Controller { public function test_composer() { $excel = new \SimpleExcel\SimpleExcel('xml'); // instantiate new object (will automatically construct the parser & writer type as XML) $excel->writer->setData( array ( array('ID', 'Name', 'Kode' ), array('1', 'Kab. Bogor', '1' ), array('2', 'Kab. Cianjur', '1' ), array('3', 'Kab. Sukabumi', '1' ), array('4', 'Kab. Tasikmalaya', '2' ) ) ); // add some data to the writer $excel->writer->saveFile('example'); // save the file with specified name (example.xml) // and specified target (default to browser) } }
Now if we go to http://localhost/welcome/test_composer we should get a download file (example.xml). It is not the scope of this tutorial to teach how to use the package so I won’t go deeper into this matter, but we have the proof of concept for using CodeIgniter with Composer.
Hi! thanks for this tutorial. I made it through here but I have one question as of now coz I always get an error in the part when installing composer. I followed all instructions and successfully installed composer in my computer by using cmd. Composer is now installed in C:\Users\Fil . Now, the problem is when every time i navigate to C:/wamp/www/directoryname/application using command line the system says “The system cannot find the path specified.”. Please guide me
Hello,
Thanks but I already solved it. Thanks anyway
Regards!
Thanks it works.
Helped me on my way, note that we can just do $excel = new SimpleExcel(‘xml’); as we are doing the use namespace. With your setup apparently use SimpleExcel\SimpleExcel; is not needed.
Indeed 🙂
hi… i have started learning well from your unique great tutorials. but in this tutorial, i have installed composer (windows) using composer_setup.exe but after that i didn’t understand what to do by the line, ‘Once you know you are there, you simply do a composer install .’ and i don;t have any vendor directory inside application….Any help?
Hello. What I meant there is that, after you create composer.json file inside “application” directory, you open command prompt and go to your “application” directory (where you’ve created composer.json). When you got there, you write at the command prompt “composer install”. After that, the composer should install the default directories in there.
ok…got it…Thanks.
I am using cPanel with CloudLinux hosted with Namecheap shared web hosting.
I have shell access but I can’t install composer properly.
When I try to install it using PHP I am getting an error message as follows:
Some settings on your machine make Composer unable to work properly.
Make sure that you fix the issues listed below and run this script again:
The suhosin.executor.include.whitelist setting is incorrect.
Add the following to the end of your `php.ini` or suhosin.ini (Example path [for Debian]: /etc/php5/cli/conf.d/suhosin.ini):
suhosin.executor.include.whitelist = phar
The php.ini used by your command-line PHP is: /usr/local/lib/php.ini
If you can not modify the ini file, you can also run `php -d option=value` to mo dify ini values on the fly. You can use -d multiple times.
————————————————————————————————————————————–
How can I fix this? Please advice.
The answer is already given in the error report… Anyway, I will approve the comment, even though I personally can’t help you. Maybe a visitor can help you.