Best approach To include 3rd party files with Symfony2

Go To StackoverFlow.com

6

I would like to know What is the best way to include 3rd party php files in symfony2. I am using a different php - ajax package for uploading files in my symfony2 application. The package offers me some php oops code which i need to use in my symfony controller. I am creating objects of that code in my controller. So i would like to know where i can put that third party code or file and how can i include or create objects of that code in my symfony2 controller. Do we use require or include in symfony2 as well. If So is that the only approach.

2012-04-04 07:55
by ScoRpion


7

I'm not so sure about trying to add namespaces to a third party library. Twig, for example, does not use name spaces. And there really is no need. Consider for example a case where you want to use the PDF component from the Zend_Framework 1 library.

In your app/autoload.php file you would do something like:

$loader->registerPrefixes(array(
    'Twig_Extensions_' => $ws . 'Symfony/vendor/twig-extensions/lib',
    'Twig_'            => $ws . 'Symfony/vendor/twig/lib',
    'Zend_'            => $ws . 'ZendFramework-1.0.0/library',
));

// And since Zend internally uses require/include we need to set an include path
ini_set('include_path','.' .

    PATH_SEPARATOR . $ws . 'ZendFramework-1.0.0/library'

);

At this point we should be able to create 3rd part objects inside of controllers while letting the autoload system take care of finding and including the classes:

    $page = new \Zend_Pdf_Page(\Zend_Pdf_Page::SIZE_A4);
    $doc->pages[] = $page;

    $font1 = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_HELVETICA);
    $font2 = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_COURIER_BOLD);

You do have to use the \ to get around the lack of namespacing.

This answer does assume that your 3rd part library follows the more or less standard class naming convention. If it has it's own auto loading functionality then just call it from autolaod.php as well. And if you don't want to use autoloading at all then just set the include path and include away.

2012-04-04 13:08
by Cerad
This is the correct way to do it. If a library does not follow any naming conventions, I would suggest to start using composer (repo, symfony). It allows the mapping of any library, making autoloading possible for even those libs not following the convention - gilden 2012-04-05 07:47


3

The documentation explains the directory structure in detail.

Basically, you can put them wherever you want, but for the sake of consistency and following best-practices, you should put your third-party libraries in vendor/ directory.

Than you can include the relevant files with namespaces.

2012-04-04 09:16
by Hakan Deryal
Great answer! I had this situation a while back and resolved it exactly like this.. - Jovan Perovic 2012-04-04 09:18
@Hakan Deryal. Thanks For Your response, Can you please tell me if i created a folder as thirdparty in vendor and place my thirdparty file there such that vendor/thirdparty/myfile.php how could i include it in my Acme Bundle - ScoRpion 2012-04-04 09:56
Is the third party application using namespaces? You can check it by looking for a line at the top of the file like 'namespace .... - Hakan Deryal 2012-04-04 10:44
Ads