As I found out on a project just a little while ago, CodeIgniter 3 does not play nice with custom “libraries” that use PHP namespaces and are directly loaded via the “libraries” folder.

This is a problem because several PHP based API services use namespaces both to initialize the library code and to interact with the API.  Case in point: Cloudinary and it’s PHP API library.

If you try to load and use Cloudinary’s PHP library the normal way in CodeIgniter, it breaks due to the heavy use of namespaces both to setup the API connection and throughout the library itself.

To resolve this issue, I wrote an easy to use solution that indirectly loads Cloudinary’s library via a dummy library file.  The API library keeps using it’s namespaces and everything is right with the world again. 🙂

It’s easy to implement and the method will work with any library that makes use of namespaces.

Visit the Project page for a demo or checkout the project on GitHub.

Join the Conversation

4 Comments

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

  1. Thank you so much for this library, it really helped me in a university project and is one of the easiest ways to integrate Codeigniter with cloudnary.
    Thanks for your work

  2. Thanks for this library.

    However, I only managed to make it work partially.

    This works fine:
    $this->load->library(‘cloudinarylib’);
    echo cl_image_tag($src, $imgatrr);

    However, I couldn’t call any function from the api itself. I tried using the Codeigniter library syntax, like this:
    $this->cloudinarylib->delete_resources_by_prefix(“xxxtodelete2”);
    but it gives the error:
    Call to undefined method Cloudinarylib::delete_resources_by_prefix()

    What am missing here?

    1. Hey Einav,

      Thanks for your comment. You make an excellent point in that it wasn’t very clear how to use the rest of the Cloudinary library in my examples.

      That said, I have updated the cloudinary-for-codeigniter github repo with additional instructions for using the rest of the Cloudinary library within CodeIgniter and also updated and tweaked the example CodeIgniter code as well too make it easier to work with.

      Basically once the library is loaded in the class you can access the rest of the API library using the “\Cloudinary\” namespace like so:

      $data[‘imageupload’] = \Cloudinary\Uploader::upload(“https://kojiflowers.com/wp-content/uploads/2017/01/vide-1050×478.png”);

      This lines it up with the Cloudinary PHP API examples.

      Let me know if you still have questions.

      1. Thanks, Koji! Now all is working flawlessly.

        Might be worth mentioning that the syntax for calling non-static functions is a bit different:

        $api = new Cloudinary\Api();
        $result = $api->delete_resources_by_prefix(“xxxtodelete”);