本文介绍了包含symfony2文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在使用google drive api在symfony2中进行捆绑。我在Utils文件夹中有一个类:与谷歌文件进行交互的认证(我把它放在完全相同的文件夹中),我想将这些文件包含在我的Authentication.php中。 我包含这样的内容: 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'; 但是我得到这个错误: 解决方案当你构建一个Bundle时,你应该使用框架函数和使用集成的自动加载器。 不要打架 在你的情况下,我更喜欢捆绑包中的服务文件夹。然后你可以把你的谷歌类放在该文件夹中,并构建一个代理类,它位于正确的命名空间中并抽取你的谷歌代码。 在Service类中,可以导入lib与要求或您可以加载您的来源通过作曲家。 <?php 命名空间MySF2Bundle \ Service;我们在这里使用最简单的方式。 require_once __DIR __。'/ google-api-php-client / src / Google_Client.php' ... 类GoogleAPIWrapper { public函数getGoogleDriveConnection(){ / ** *实现Google驱动器函数 * / } } 然后,您可以在导入命名空间时在Bundle中使用它: 使用MySF2Bundle \Service\GoogleAPIWrapper; 使用此方法,您可以从捆绑代码中抽取Google API,并且可以使用更好的结构。它有可能导致命名空间出现问题。但是你可以测试它。 否则你可以看看Github上的其他包如何实现外部库。 这是另一种方式: http://www.kiwwito.com/article/add-third-party-libraries-to-symfony-2 向Symfony2项目添加外部库 $ b $因此,您可以实现完整的库并在symfony2自动加载器中加载库。 $ loader-> registerPrefixes(数组('Google'=> __DIR __。'/ .. / vendor / Google / Drive /',)); 所以你会发现有一些可能性来实现一个外部库。 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: 解决方案 When you build a Bundle you should work with the framework functions and use the integrated autoloader.Don't fight the frameworkIn 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.<?phpnamespace 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-2Add external libraries to Symfony2 projectSo 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. 这篇关于包含symfony2文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 18:28