2014-02-27 28 views
1

我是Zend Framework的新手。我爲Google日曆添加了一堆API類到/vendor/文件夾。然後,我編寫了一個自定義課程,用於對我的Google日曆帳戶進行身份驗證和添加活動。並將其放在/模塊/應用/ src /應用/Util/GoogleAgenda.phpZf2在項目中加載自定義類

所以。現在在EventController(在模塊/事件/ src目錄/事件/ EventController.php)我想使用的自定義GoogleAgenda類是這樣的:

$agenda = new \GoogleAgenda(); 
    if($agenda->auth()) { 
     $success = $agenda->addEvent($event->serviceName, "", $event->locationName, "2014-02-27T17:00:00.000+00:00", "2014-02-27T18:00:00.000+00:00"); 
     if(!$success) { 
      die("Google Calendar Api - Failed to add event"); 
     } 
    } 

這給了錯誤:

Fatal error: Class 'GoogleAgenda' not found in EventController.php on line 60

所以,在這裏把我的定製GoogleAgenda類?以及如何從另一個模塊中的控制器類加載它們。

我以爲我的Util文件夾是存儲這個類的好地方。因爲它是一個我希望在整個應用程序中使用的util類。

回答

3

您需要將該類自動加載。既然你已經把它放在你的應用程序模塊中,那麼你對該模塊的命名空間和自動加載器設置最簡單。由於該類住在module/Application/src/Application/Util/GoogleAgenda.php,它的聲明應該是這樣的:

<?php 

namespace Application\Util; 

class GoogleAgenda 
{ 
    [...] 
} 

和使用,這將是:

$agenda = new \Application\Util\GoogleAgenda(); 

,或者更好一點:

use Application\Util\GoogleAgenda; 

[...] 

$agenda = new GoogleAgenda(); 

And how to load them from a controller class in another module

上面的代碼應該可以在你應用程序的任何模塊中工作。

+0

謝謝蒂姆,它的工作原理!我還注意到,我需要在調用vendor \ google-api \文件夾中的類之前添加一個「\」。像$ this - > _ client = new \ GoogleClient();否則,它會在新的命名空間Application \ Util中查找該類。乾杯! – Floris

+1

是的,或者像上面我的例子那樣在PHP腳本的頂部添加'使用GoogleClient'。 –

相關問題