Use Google Drive Api in Laravel 5

2019-05-13 03:35发布

问题:

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!

回答1:

I think I have it.

I only have to set:

use Google_Client; 
use Google_Service_Drive;


回答2:

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"
        ]
}