Question

I would like to get some help with symfony2 regarding the bundle registration in the Kernel,

Ive read most of symfony2 cookbook and doc for exemple : How to install 3rd party Bundles

But yet everytime I failed doing so. I am using symfony 2.3 with composer, both up to date.

Today I would like to use a 3rd party bundle called "google/apiclient" found on packagist.org, therefor i add the following line "google/apiclient": "dev-master" to my composer.json file which is located at the root of my project.

Followed by a composer.phar update in order to get the bundle which was downloaded with success and installed in the vendor repository

I belive that so far im doing okay, but the next step is where i get lost.

Next I belive ive to register this new bundle in the app\AppKernel.php like the other one, for exemple:
new Symfony\Bundle\TwigBundle\TwigBundle()

But I don't know what to write for the new google\apiclient ...
So first question is what should I write there and how do you figure it out ?

Once this is done properly, I belive that i'm now able to use it in a controller, but my second question is :
How do i load the Client.php which is located in vendor/google/apiclient/src/Google ?

The point of all of this, is to be able to sth like the given exemple in my controller:

 require_once 'Google/Client.php';
  require_once 'Google/Service/Books.php';
  $client = new Google_Client();
  $client->setApplicationName("Client_Library_Examples");
  $client->setDeveloperKey("YOUR_APP_KEY");
  $service = new Google_Service_Books($client);
  $optParams = array('filter' => 'free-ebooks');
  $results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);

  foreach ($results as $item) {
    echo $item['volumeInfo']['title'], "<br /> \n";
  }

The goal is to be able to use the google contact api in a symfony2 controller. Any help or suggestion welcome, cheers

Was it helpful?

Solution

For people who like me couldn't figure out how to load a 3rd party library in a controller,

Here is what a did :

I wanted to use the google/apiclient found on packagist.org

  1. I add the following line in "google/apiclient": "dev-master" under "require" to my composer.json file which is located at the root of my project.

  2. Install the new library with composer ( in my case i used the command)
    php composer.phar update

and it's done lol ...
it's really that easy, the only thing left is to load it in your controller by calling the class directly.
For exemple in my case.
I wanted to $client = new Google_Client();,
I thought I had to use require_once 'Google/Client.php';
but not at all if you used composer to install the library you can find that /vendor/composer/autoload_classmap.php return an array with the following line
'Google_Client' => $vendorDir . '/google/apiclient/src/Google/Client.php',

So all I have to in my symfony controller is to :
IMPORTANT
use Google_Client;
and then $client = new Google_Client(); and composer load it for me from its autoload_classmap.php
Or directly write $client = new \Google_Client();
I hope my experience will help some of you, Cheers, and thanks to @Touki and @waldek_c .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top