Step 7 – Using CodeIgniter and Composer – with a real world example

Lately, everyone wants some good (if not great) piece of code from – let’s say – 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 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:

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:

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 Someone asked on the CodeIgniter Forum, how to use Simple Excel PHP. So why not try this one?

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:

After we save the composer.json file, we go again into our application directory from the command prompt and write:

Once we’ve done that we should have a faisalman directory inside our application/vendor directory:

Now let’s test it.

For testing reasons let’s just make a test_composer() method inside our Welcome class/controller (Welcome.php):

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:


Of course, we can skip the namespace declaration, but telling you about namespaces is not the scope of this tutorial:

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.

0 thoughts on “Step 7 – Using CodeIgniter and Composer – with a real world example

  • 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

  • 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.

  • 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.

  • 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

No spam? * Time limit is exhausted. Please reload CAPTCHA.