Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including files with symfony2

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'

like image 872
patricia Avatar asked Dec 20 '22 13:12

patricia


1 Answers

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.

like image 131
René Höhle Avatar answered Jan 08 '23 11:01

René Höhle