0

I am having a hard time understanding Symfony2 services. I have read lots of stuff everywhere (including some here in SO) but none seems to fully explain it.

Suppose I have a bundle A and a separated bundle B. I want B functionality available to the A bundle. I want to inject B in the service container so A will be able to use it.

Which bundle should have a Services directory? Which one should have a configuration file? Both if needed? And where the Extension goes? Why?

phasmatis
  • 1
  • 1

2 Answers2

3

Bundle B will require an Extension in order to load it's services.xml file.

Bundle B will require an entry in it's services.xml file to define the service.

Bundle B will have the Services directory containing your service class which exposes the desired functionality.

Bundle A does not require anything special. It will be able to use the container to access the service exposed by Bundle B. Just needs to know the service id.

It's confusing until you make a few services.

Cerad
  • 48,157
  • 8
  • 90
  • 92
  • Thanks, it makes some sense now. Even though I didn't test yet this is by far the most effective explanation (if it is correct) I have read. – phasmatis Apr 13 '12 at 20:19
1

Read these two questions and my answers to them first:

Assuming you're talking about app specific bundles, I suggest having one bundle only and keep services out it. Then, you could register your services in several ways:

  • Directly in the config.yml,
  • Creating an extension class in your AppBundle, or
  • Via annotations from JMSDiExtraBundle — this is what I prefer personally.
Community
  • 1
  • 1
Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133