We help IT Professionals succeed at work.

Symfony 2.8, bundles, writing a service

Medium Priority
Last Modified: 2019-04-25
We use Symfony 2.8 as the framework for our website. The people who set it up years ago probably didn't do it right, and we have about 10 different bundles which comprise all the different areas of our site (Cart, MyAccount, Products, Pages, etc).

Two questions:

Should the entire website be under just one bundle, so we dont have duplicated code in multiple bundles?


If a bundle needs a bit of code to get a piece of data, should that be written as a service so we can call it from anywhere, without having multiple lines of code duplicated all over the place wherever we need that piece of data?
Watch Question

Top Expert 2004
The real answer: it depends.

Bundles were meant to be whole sections of functionality, generally (but not necessarily) optional to the core project.  For example, if you buy a car, the core consists of your engine, exhaust, electrical, etc.  Air conditioning and the sound system would be bundles you could add to the car.  You could argue that those two bundles are so important to the car that they aren't really optional, but it is a *functional* distinction - the car can run without them.  In the context of your website, it could be wholly appropriate to organize different features into bundles.  Again, some bundles may be more critical than others, i.e., not really optional, but it is more a focus on the packaging of functionality.

Regarding duplicate code, I'm a firm believer in DRY.  If you have code used in multiple places - a distinct but commonly required piece of functionality - then creating a service for it is the right way to go.  That makes sure you have ONE set of code determining how a particular thing works.  That is better for management and development.  Any functionality common to your project as a whole should be a core service, making it available to any bundles you add.  Functionality provided by a bundle, but potentially consumable by other areas (think plugins, and the like) might be a service in the appropriate bundle.  Other consumers would check if the service is available and respond accordingly.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.