Best Approach To Include Third-Party Files With Symfony2

I would like to know What is the best way to include third-party php files in symfony2. I am using another php-ajax package to upload files to a symfony2 application. The package offers me some php oops code that I need to use in my symfony controller. I create objects of this code in the controller. Therefore, I would like to know where I can place this third-party code or file, and how I can include or create objects of this code in my symfony2 controller. Whether we use use or inclusion in symfony2. If So is the only approach.

+6
source share
2 answers

I'm not sure about trying to add namespaces to a third-party library. For example, Twig does not use namespaces. And this is not necessary. Consider, for example, the case when 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 moment, we should be able to create objects of the 3rd part inside the controllers, allowing the startup system to take care of the search and inclusion of 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 need to use \ to get around the lack of a namespace.

This answer assumes that your 3rd part library conforms to more or less standard class naming conventions. If it has its own auto-download feature, just call autolaod.php. And if you don’t want to use autoload at all, just set the enable path and enable it.

+7
source

The documentation explains the directory structure in detail.

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

Than you can include the corresponding files with namespaces.

+3
source

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


All Articles