且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Symfony2.3,如何在内核中注册第三方包,并从Controller加载它?

更新时间:2023-11-19 21:24:46

对于像我这样无法理解如何在控制器中加载第三方库的人,

For people who like me couldn't figure out how to load a 3rd party library in a controller,

这是做什么的?

我想使用在 packagist.org

  1. 我将"google/apiclient": "dev-master"下的以下行添加到位于项目根目录的composer.json文件中的要求"下.

  1. I add the following line in "google/apiclient": "dev-master" under "require" to my composer.json file which is located at the root of my project.

使用composer安装新库(在我的情况下,我使用命令)
php composer.phar update

Install the new library with composer ( in my case i used the command)
php composer.phar update

完成了大声笑...
真的很简单,剩下的唯一事情就是直接调用该类将其加载到您的控制器中.
以我的情况为例.
我想$client = new Google_Client();
我以为我必须使用require_once 'Google/Client.php';
,但是如果您使用composer安装库,则根本不用,您会发现/vendor/composer/autoload_classmap.php返回以下行的数组
'Google_Client' => $vendorDir . '/google/apiclient/src/Google/Client.php',

and it's done lol ...
it's really that easy, the only thing left is to load it in your controller by calling the class directly.
For exemple in my case.
I wanted to $client = new Google_Client();,
I thought I had to use require_once 'Google/Client.php';
but not at all if you used composer to install the library you can find that /vendor/composer/autoload_classmap.php return an array with the following line
'Google_Client' => $vendorDir . '/google/apiclient/src/Google/Client.php',

所以我在symfony控制器中要做的就是:
重要
use Google_Client;
然后$client = new Google_Client();和作曲家从autoload_classmap.php
为我加载它 或直接写$client = new \Google_Client();
希望我的经验对您有所帮助, 干杯,感谢@Touki和@waldek_c.

So all I have to in my symfony controller is to :
IMPORTANT
use Google_Client;
and then $client = new Google_Client(); and composer load it for me from its autoload_classmap.php
Or directly write $client = new \Google_Client();
I hope my experience will help some of you, Cheers, and thanks to @Touki and @waldek_c .