Including files with symfony2

I am making a package in symfony2 using google drive api. I have a class in the Utils folder: Authentication, which interacts with files from Google (which I put in the same folder), and I want to include these files in my Authentication.php.

I include the following:

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 to open "google-api-php-client \ src \ Google_Client.php"

+4
source share
1 answer

When you create a bundle, you have to work with the functions of the framework and use the built-in autoloader.

Do not fight with the frame

In your case, I would prefer the Tools folder in your Bundle. Then you can put your google classes in this folder and create a proxy class that is in the correct namespace and abstract google code.

In the Service class, you can import your library with the requirement or load your sources on top of the composer. We use the easiest way here.

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

You can then use it in your Bundle when importing a namespace:

 use MySF2Bundle\Service\GoogleAPIWrapper; 

With this method, you can abstract the Google APIs from your Bundle code, and you can work with a better framework. You may have encountered problems in namespaces. But you can check it out.

Otherwise, you can look at other Github nodes 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 the Symfony2 project

So you can implement the full lib library and load the library into the symfony2 autoloader

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

So, you see that there are some possibilities for implementing an external library.

+11
source

Source: https://habr.com/ru/post/1447015/


All Articles