2012-11-19 33 views
4

我正在用symfony2與Google Drive api進行捆綁。我在Utils文件夾中有一個類:與來自Google的文件進行交互的認證(我把它放在完全相同的文件夾中),我想將這些文件包含在我的Authentication.php中。包含symfony2的文件

我有這樣的:

require_once 'google-api-php-client/src/Google_Client.php'; 
require_once 'google-api-php-client/src/contrib/Google_DriveService.php'; 
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php'; 

但我得到這個錯誤:

Fatal error:main() : Failed opening required 'google-api-php-client\src\Google_Client.php'

+0

@ stony你對這個問題有更多的想法嗎? – patricia

回答

11

當你構建一個包,你應該與框架職能的工作,並使用集成的自動加載。

不打框架

你的情況,我寧願服務文件夾中捆綁。然後,你可以把你的谷歌類放在該文件夾中,並構建一個代理類,它位於正確的命名空間中,並將你的谷歌代碼抽象出來。

在服務類中,您可以使用require導入您的庫,或者您可以通過作曲者加載源代碼。我們在這裏使用最簡單的方法。

<?php  
namespace MySF2Bundle\Service; 

require_once __DIR__.'/google-api-php-client/src/Google_Client.php' 
... 

class GoogleAPIWrapper { 
    public function getGoogleDriveConnection() { 
     /** 
     * Implement the Google drive functions 
     */ 
    } 
} 

然後,當你導入命名空間,你可以用它在你的包:

use MySF2Bundle\Service\GoogleAPIWrapper; 

使用這種方法,你可以抽象的谷歌API從捆綁的代碼,你可以用一個更加合理的結構工作。它有可能導致命名空間出現問題。但是你可以測試它。

否則,你可以看看Github上的其他包如何實現外部庫。

這裏是另一種方式:

http://www.kiwwito.com/article/add-third-party-libraries-to-symfony-2 Add external libraries to Symfony2 project

這樣你就可以實現完整的lib和在Symfony2中自動加載

$loader->registerPrefixes(array(
    'Google' => __DIR__.'/../vendor/Google/Drive/', 
)); 

所以你看有一些加載庫實現外部庫的可能性。

+0

服務文件夾與我做的不一樣(不同之處在於文件夾的名稱有所不同)?有什麼不同? 對不起,如果這是一個愚蠢的問題,但我不認爲我明白你的意思。 – patricia

+0

嘗試用'__DIR__'或'dirname(__ FILE __)'來定義你的路徑' – Stony

+0

我需要在所有需要的google api文件中做dirname(__ FILE__)嗎? – patricia