2015-04-08 62 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