Silex/Google API

455 views Asked by At

I am working on a Silex application, trying to get the Google API PHP client implemented via Composer, and having little luck. I've tried a number of different configurations, including variations in app.php, autoload_namespaces.php, autoload_classmap.php as well as in the Google_Client class itself.

I load the library with:

"require": {
    "google/apiclient": "^1.1",
    ...
}

Then I add:

"autoload": {
    "psr-0": {
        "Google\\Client": "vendor/google/apiclient/src/"
    }
},

to get the library in autoload_namespaces.php

In app.php I have:

use Google\Client;
...
$app->register(new Client());

Which gives me "Fatal error: Class 'Google\Client' not found in...' Part of the problem seems to be that the Google library uses underscores in its classnames. When I remove 'Google_' from 'class Google_Client' in the library, the error changes, but still no cigar...

So I think this has something to do with the use of underscores and the naming of classes in the google library. Is there another Composer configuration that I can use to resolve this issue? Or another Silex workaround to be able to access this library?

Thanks!

1

There are 1 answers

1
mTorres On BEST ANSWER

You should never add your dependencies in your autoload map (composer does that for you). In this case, google PHP API client is not namespaced so composer can't autoload the library using namespaces.

In order to use it you just need to append the root namespaces before the class:

<?php

require 'path/to/vendor/autoload.php';  // remember to include composer's autoload file

$gclient = new \Google_Client();