Q
Q
Quber2013-11-19 10:25:30
symfony
Quber, 2013-11-19 10:25:30

Symfony2: How to add a class from the vendor folder to the service container?

Actually a subject. I downloaded a third party library from GitHub. Uploaded to the vendor folder. Connected in autoload a new namespace. I want to call in the controller like this: so far it only works like this:
$foo = $this->get('bar');

use \bar;
$bar = new Bar();

It worked for me through the services.yml file, but only if the library is in a bundle, then we write the namespace in the path and that's it. If the library is outside the bundle (like mine), ala vendor, then this does not work anymore:
services:
    foo:
        class: \Bar

Please tell me how to define a class from the vendor folder as a service! Or is it necessary to transfer the library to the bundle? Thank you in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zloyusr, 2013-11-19
@Quber

For a container, it doesn't matter where the file is physically located. That's up to the autoloader. If you have registered the service in services.yml:

services:
    fooService:
        class: \Bar

Then it will be available in the controller:
$foo = $this->get('fooService');
But to integrate a third-party library, I would recommend creating a separate bundle and storing all the services configuration in the services.yml of this bundle.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question