Where to store a simple third-party class in Symfony2?

I'm new to Symfony2, and I am having some simple problems, but I'm not sure how to handle this. I need to use one simple third-party class, and I'm not sure where and how to save it in the project structure. Should I store the service in my package or maybe I should store it in the suppliers directory? And if I store it with sellers, is it not a bad practice to store libraries that are not Symfony-compatible providers there?

+6
source share
3 answers

Typically, you include Composer tags in your project. I suggest you take a look at packagist to see if there is a Composer package for your class, otherwise you cannot require it using a composer.

Composer places your classes in the vendor directory, you must place all the "providers" there (3rd party library). See where to put them in this directory so that the Composer autoloader can automatically download it.

After that, it is recommended to create a package for this particular class. This is the best way to create a service there. For example, if your class is Foo , you create Acme\FooBundle , which loads the Foo service:

 // src/Acme/FooBundle/DependencyInjection/AcmeFooExtension.php <?php namespace Acme\FooBundle\DependencyInjection; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\HttpKernel\DependencyInjection\Extension; class AcmeFooExtension extends Extension { /** * this method loads the Service Container services. */ public function load(array $configs, ContainerBuilder $container) { $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); // load the src/Acme/FooBundle/Resources/config/services.xml file $loader->load('services.xml'); } 
 <!-- src/Acme/FooBundle/Resources/config/services.xml --> <?xml version="1.0" encoding="UTF-8" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> <services> <!-- Loads the \Foo class as a acme_foo.foo service --> <service id="acme_foo.foo" class="\Foo" ></service> </services> </container> 
+4
source

Symfony itself stores third-party libraries in the vendors folder. It is good practice to post your third-party class there

If you do not know how to do this, perhaps this question will help.

+1
source

I think using a service container would be good practice. In any case, the service container is designed to store third-party options and maintain free communication.

Look at the docs , it is written how and why the service container should be used.

Good luck.

+1
source

All Articles