4

I'm a newbie in Laravel, and surely this question has an obvious answer, but I've not been able to connect Laravel 5 with Google Api.

I have install the Api with composer like always, and it is in my vendor folder, but now I'm not sure how to use it.

I've not found an answer to this (because this must be extremely simple).

Probably I'm missing a namespace call, or something like this.

On my IndexController, I have:

    <?php 

namespace App\Http\Controllers;

class IndexController extends Controller {

    /**
     * Show the application welcome screen to the user.
     *
     * @return Response
     */
    public function index()
    {
      $client = new Google_Client();
      $client->setApplicationName("Client_Library_Examples");
      $client->setDeveloperKey("HERE_GOES_MY_KEY");

      $service = new Google_Service_Drive($client);
      $results = $service->volumes->listVolumes();

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

}

And the error I get is:

Class 'App\Http\Controllers\Google_Client' not found

I thought that it could be a problem with autoload_classmap, but there are all GoogleApi classes defined, like:

(...)

'Google_Client' => $vendorDir . '/google/apiclient/src/Google/Client.php',

(...)

'Google_Service_Drive' => $vendorDir . '/google/apiclient/src/Google/Service/Drive.php',

Thanks for your patience and your help!

user3592436
  • 461
  • 2
  • 4
  • 13

2 Answers2

18

I think I have it.

I only have to set:

use Google_Client; 
use Google_Service_Drive;
user3592436
  • 461
  • 2
  • 4
  • 13
  • 1
    Or you could use the fully-namespaced class names, if you're only going to use them once. It looks like `$client = new \Google_Client()` would be fine. Normally the root namespace \ is the default, but since you are in a custom namespace you need to specify the different one, either using your approach or mine. You can see that PHP assumed the un-namespaced class could be found in `App\Http\Controllers` - the same namespace as your current class. – halfer Apr 08 '15 at 09:16
0

check the file composer.json

and add "vendor/google/apiclient/src/Google" in classmap array if not exist.

and run composer dump-autoload

"autoload": {
        "classmap": [
            "vendor/google/apiclient/src/Google"
        ]
}
tushar zore
  • 101
  • 1
  • 8