2015-04-08 176 views
3

我是Laravel的新手,当然这个问题有一个明显的答案,但我一直无法将Laravel 5与Google Api连接起来。在Laravel 5中使用Google Drive Api

我已经像往常一样安装Api作曲家,它在我的供应商文件夹,但现在我不知道如何使用它。

我还没找到答案(因为这一定非常简单)。

也许我缺少名称空间调用或类似的东西。

在我的IndexController,我有:

<?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"; 
     } 
    } 

} 

而我得到的错误是:

级 '应用程序\ HTTP \控制器\ Google_Client' 未找到

我认为这可能是一个autoload_classmap问题,但有定义的所有GoogleApi类,如:

(...)

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

(...)

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

感谢您的耐心和您的帮助!

回答

10

我想我有。

我只需要设置:

use Google_Client; 
use Google_Service_Drive; 
+1

或者你可以使用全名称空间的类名,如果你只打算用一次。它看起来像'$ client = new \ Google_Client()'会很好。通常情况下,根名称空间\是默认的,但由于您处于自定义名称空间,因此您需要指定不同的名称空间,无论是使用您的方法还是我的方法。你可以看到,PHP假设了非命名空间类可以在App \ Http \ Controllers中找到 - 与当前类相同的命名空间。 – halfer