I am trying to use composer to autoload a third party library into my ZF2 application - specifically Google api. I followed the answer in this post on SO, edited my composer.json
"autoload": {
"psr-0": {"Googleanalytics\\": "vendor/google-api-php-client/src/"}
}
and ran update. I can see the entry in composer/autoload_namespaces.php
'Googleanalytics\\' => array($vendorDir . '/google-api-php-client/src'),
but i still get a fatal error class not found when trying to instantiate a class in that directory (Google_Client.php).
Any ideas what i am missing? I am including the file in the class i am trying to use it:
use Googleanalytics\Google_Client;
I have tried renaming the directory in case the - was the problem and also creating a simple test.php file in that dir in case the underscore in the class name (Google_Client.php) was the problem, but still the same error.
Is there anything else i need to add to my ZF2 application to autoload this library? Also note i decided not to use ZendGdata as this component does not seem to be maintained anymore. Thanks in advance
For anyone else looking to add a third party library to ZF2 using composer, here are the steps that worked for me.
Add following line to composer.json
"autoload": { "classmap": ["vendor/PATH TO LIBRARY"] }
Run php composer.phar update
Then you should see all the classes that were in the 3rd party library in the file in the composer folder: composer/autoload_classmap.php
When instantiating any class from the library in your zf2 application, dont forget to prefix the class name with a \. For example:
The autoload definition of your software should not include the autoload definition of any vendor module. Move that to the package definition you use to include the software.
And in other news: If it does not work with PSR-0, the classmap autoloader should take care of it.
Update
How to create the package for a repository not offering a composer.json
Essentially you'd need only a couple of pieces of information:
In case of the "google-api-php-client", the a) URL of the repository is
http://google-api-php-client.googlecode.com/svn/
, the b) most current version number is0.6.7
, the A) download URL of that package ishttp://google-api-php-client.googlecode.com/files/google-api-php-client-0.6.7.tar.gz
.And now you fill it into this "template":
And then you can require that exact package in your requirements:
"require": { "name from (2)": "version from (1)" }
For the google package you are using this would essentially get you to use this:
The benefit of adding this mostly boilerplate stuff is that you get the downloading of the software for free now. You don't have to care about how to manually download, unpack and install the package. You did add the autoloading information for this software to your own composer.json, but it is contained in the package definition of the software you want to use, it is not contained in the autoloading area of your own software.
You also do not have to worry about Composer removing your manually downloaded package accidentally.