Including files with symfony2

后端 未结 1 1096
忘了有多久
忘了有多久 2021-02-06 14:21

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 fo

相关标签:
1条回答
  • 2021-02-06 14:44

    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.

    0 讨论(0)
提交回复
热议问题