4

I'm doing a bundle in symfony2 with google drive api. I have a class in Utils folder: Authentication which interact with the files from google (that I put in that exact same folder) and I wanna include those files in my Authentication.php.

I include like this:

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';

But I get this error:

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

Claudio Cherubino
  • 14,896
  • 1
  • 35
  • 42
patricia
  • 1,075
  • 1
  • 16
  • 44

1 Answers1

11

When you build a Bundle you should work with the framework functions and use the integrated autoloader.

Don't fight the framework

In your case i would prefer a Service folder in your Bundle. Then you can put your google classes in that folder and build a Proxy class which is in the correct namespace and abstract your google code.

In the Service class you can import your lib with require or you can load your sources over composer. We use here the easiest way.

<?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
        */
    }
}

Then you can use it in your Bundle when you import the namespace:

use MySF2Bundle\Service\GoogleAPIWrapper;

With this method you can abstract the Google API from your Bundle Code and you can work with a better structure. Its possible that you get some trouble with the namespaces. But you can test it.

Otherwise you can look on other bundles on Github how they implement external libraries.

Here is another way:

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

So you can implement the complete lib and load the library in the symfony2 autoloader

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

So you see there are some possibilities to implement an external Library.

Community
  • 1
  • 1
René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • The service folder isn't the same thing that I did inicially (the difference is the name of the folder that changes)? what's different? I'm sorry if it's a stupid question but I don't think I understand what you've said. – patricia Nov 19 '12 at 17:07
  • Try to define your path with `__DIR__` or `dirname(__FILE__)` – René Höhle Nov 19 '12 at 17:08
  • Do I need do put dirname(__FILE__) in all requires of google api files? – patricia Nov 19 '12 at 17:22
  • The problem is when you don't use the autoloader you don't know were you are. So i wand use `dirname(__FILE__)` relative from your file position. – René Höhle Nov 19 '12 at 17:22
  • I made the Services folder as you said but I still get an error. I print the path I'm writing to include the googles files and It's correct: `require_once __DIR__."/google-api-php-client/src/Google_Client.php";` the echo of this path is: `/var/www/Symfony/src/Files/GoogleDriveBundle/Services/google-api-php-client/src/Google_Client.php` – patricia Nov 23 '12 at 14:14
  • 1
    I think then your Path is not correct. you should think about when you call the class you need a "\" before the class for the root namespace. – René Höhle Nov 24 '12 at 11:02
  • That's it! The \ before the class solve my problem. Thank you :D – patricia Nov 26 '12 at 14:13
  • 1
    no problem :) when you don't think about the namespaces you have such problems :D – René Höhle Nov 26 '12 at 14:15