How do you organize your bundles in Symfony2 projects? [closed]

怎甘沉沦 提交于 2019-12-04 03:47:25
Elnur Abdurrakhimov

Edit: I don't use bundles for app-specific code anymore.


Personally I prefer to have a bundle per a section of an application. For example:

  • UserBundle
  • BlogBundle
  • ForumBundle
  • JobBundle
  • StoreBundle
  • etc

This is okay if the app is a mishmash of several functionalities, none of which is big enough to require a separate application and/or a subdomain. But if I was developing a big webstore applicaton, my bundles would be more specific:

  • UserBundle
  • ProductBundle
  • CartBundle
  • SearchBundle
  • WishlistBundle
  • etc

So, I'd say, it depends on the focus of the project. What's just a section for one project could be the core functionality of another.

And I usually have CommonBundle, where all the common stuff goes, like global CSS, images, layouts, etc.

Also there are at least two options for the backend organization:

  1. each bundle has its own backend section, or
  2. there is one big backend bundle.

Personally I lean towards the first option and you can read about it in my previous answer, but there are people who prefer to have a separate bundle for the whole backend — probably using one of the admin bundles.

By the way, it's perfectly okay for bundles to be interconnected — you don't have to make them all independent of each other. For example, JMSDiExtraBundle depends on the metadata library and JMSAopBundle, which in turn depends on cg-library. If you'll try to keep bundles totally independent, you'll end up with big monolithic one-bundle lumps of code.

For every project I start off with one CoreBundle, where I put everything together. Then I just develop features in it and as time goes I reevaluate it - if I might use this feature somewhere else someday (or even release to open source), I move it to a new bundle.

"Size" of the feature worth separate bundle doesn't really matter - I've seen OS bundles as big as a 1 single js file :D

One thing for sure - stuffing everything in a single bundle is bad, it goes against the whole reason why this architecture was implemented in the first place!

Jérémy Dutheil

My answer in the following topic can probably help you : Symfony 2 : Location of Entities

I'm not a Symfony2 master, but I think I have quite a good idea of the bundle design ; of course, there is no universal answer but you can follow some "best practices".

First of all, I don't think that big bundles are a good solution ; you doesn't split your project into applications anymore, like you did with Symfony1.4. You may ask "but what can I do with the frontend/backend logic ?" ; quite easy, use the controller !

Each bundle should refer to a module, a stone in your project's wall. You have to divide your application ; many bundles are not bad. Of course, don't do a bundle for each entities, that would be a waste of time. But imagine a blog application : you would have an User bundle, Articles bundle (which would manage posts, categories, ...), eventually a bundle for static pages, ...

It's not unlogical that your bundle are linked ; you are building a whole application, so in this case they are linked. But the keyword here is "generalization" ; your bundle should be able to be linked to others bundle, not only yours. You should be able to re-use it in others projects.

Good luck !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!