• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact

CodeSamplez.com

Programming, Web development, Cloud Technologies

You are here: Home / Development / Using Composer With Codeigniter

Using Composer With Codeigniter

June 3, 2014 by Rana Ahsan 11 Comments

codeigniter composer

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 on my own codeigniterplus project, and if you are familiar with that project, you can check it out as well. If you are concerned to get support for Composer with CodeIgniter in an existing application, you can totally do that without much worry as well.

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 about if all of this has been 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 huge unnecessarily. 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 at all.

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 monolog as an example) you want to install:


{
	"require": {
                "monolog/monolog": "*"
	},
	"minimum-stability": "dev"
}
Code language: JSON / JSON with Comments (json)

Then being on that directory, run ‘composer update’ command on your command line tool. If all goes OK, you should notice a vendor directory and a composer.lock files are created there. Congratulation, 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 which are inside vendor directory. Composer handles all the dependencies by dynamically creating a single ‘autoload.php’ which loads all those libraries.

The Alternative Recommended Way:

Though the previous way was easy and simple enough, personally, 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 load 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 elements:


$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 a death of it as well( though I appreciate the development of codeigniter community, which is still active, but no stable release yet). In between, PHP is already updating 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 🙂

Share If Liked

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)

You may also like

Filed Under: Development Tagged With: codeigniter, composer, php

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Reader Interactions

Comments

  1. David says

    July 7, 2014 at 10:36 am

    I have added it in a helper file and added this helper to CI autoload config file

    Reply
  2. Dean Or says

    July 22, 2014 at 3:53 pm

    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.

    Reply
  3. Husnain Afzal says

    November 30, 2014 at 5:05 am

    Thanks for simple and nice tutorial.

    Reply
  4. webproger1 says

    April 7, 2015 at 7:15 am

    I think that you forgot this line after “<?php"
    ==
    defined('BASEPATH') OR exit('No direct script access allowed');
    ==
    in 'MY_Composer' lib

    Reply
  5. David Levy says

    April 7, 2015 at 2:40 pm

    In CI 3.0 there is a config line

    $config[‘composer_autoload’] = true;

    One more reason to move to this release

    Reply
  6. Irsyad says

    May 10, 2015 at 11:21 pm

    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

    Reply
    • Md Ali Ahsan Rana says

      May 11, 2015 at 11:25 am

      Seems like you are not using proper namespacing. It suppose to be ‘new Khill\Lavacharts()’ for the library you mentioned.

      Reply
      • Irsyad says

        May 11, 2015 at 9:20 pm

        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 🙂

        Reply
  7. Terry Lin says

    December 7, 2015 at 12:31 am

    I put the code in MY_Controller.php and extends CI_Controller, it also works good. Thanks for your article.

    Reply
  8. Aman says

    July 21, 2016 at 3:37 am

    Thank you mate, have been stumbling for the correct way to include libraries and have been doing it by the dirty way 😛

    Reply

Trackbacks

  1. Codeigniter + Composer says:
    September 19, 2022 at 4:04 pm

    […] Codeigniter has not support for installation via Composer. When I search in Google I found this link. It seems that Codeigniter has not support for autoload.php file created by Composer. The […]

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Follow Us

  • Twitter
  • Facebook

Top Posts & Pages

  • How To Work With JSON In Node.js / JavaScript
    How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
    PHP HTML5 Video Streaming Tutorial
  • How To Work With C# Serial Port Communication
    How To Work With C# Serial Port Communication
  • Facebook C# API Tutorials
    Facebook C# API Tutorials
  • LinQ Query With Like Operator
    LinQ Query With Like Operator
  • Using Supervisord Web Interface And Plugin
    Using Supervisord Web Interface And Plugin
  • Utilizing Config File In C#.NET Application
    Utilizing Config File In C#.NET Application
  • Getting Started With UDP Programming in Java
    Getting Started With UDP Programming in Java
  • Generate HTTP Requests using c#
    Generate HTTP Requests using c#
  • How To Use Hotkeys/Keyboard Events In WPF Application Using C#
    How To Use Hotkeys/Keyboard Events In WPF Application Using C#

Recent Posts

  • Building Auth With JWT – Part 2
  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read

Tags

.net angularjs apache api audio auth authenticatin aws c# cloud server codeigniter deployment docker doctrine facebook git github golang htaccess html5 http javascript jwt linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty socket.io tfs tips unit-test utility web application wordpress wpf

Footer

Archives

Follow Us

  • Twitter
  • Facebook

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Copyright © 2023