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?

4

2 回答 2

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.

于 2012-04-13T19:55:58.690 回答
1

首先阅读这两个问题和我对它们的回答:

假设您正在谈论特定于应用程序的捆绑包,我建议只使用一个捆绑包并将服务排除在外。然后,您可以通过多种方式注册您的服务:

  • 直接在config.yml,
  • AppBundle, 或
  • 通过来自的注释JMSDiExtraBundle——这是我个人更喜欢的。
于 2012-04-13T18:44:52.280 回答