Vendors, Packagist, and Composer in CakePHP 3

New with CakePHP 3 is the recommended use of composer. Composer makes managing dependencies easier and updating packages easier as well. Packagist is a nice resource for finding packages, but I do think it could use a bit more work such as a comment system for packages. I had some difficulties at first adding packages using composer and getting them to load in my Cake3 project. I’ll break down two examples in this blog post so you can get up and running faster.

Installing twitter-api-php

I needed to do some work with Twitters REST API. To do so I used j7mbo/twitter-api-php. This package does not have an autoloader.php file. Here are the steps I followed:

Then in my composer.json file I added the following line under the autoload configuration for psr-4:

This namespaced the package to TwitterApiExchange. In my code I can now use this as follows:

In the above example I created a component, this is not important though. The key parts here are referencing the namespace TwitterAPIExchange and then instantiating the class later on.

Installing google/apiclient

Now lets use a more complex vendor package. We’ll use the Google API Client which contains many classes. We’ll omit the composer installation step in this example and move right into our composer.json file:

With that completed we can begin using various classes within the Google API: