Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 bundles: am I using them right?

Tags:

php

symfony

I have an application which is developed in Symfony2. Now the structure for it is as follows:

  • FrontBundle - includes everything related to the application's view and UI.
  • PersistanceBundle - includes everything related to the persistence layer of the application.
  • DomainBundle - includes everything related to the entities of the application and the services.

Is this structure ok? Or bundles are used like forum feature - ForumBundle - which includes every layer (controllers, services, domain logic and persistence) related to the forum.

like image 886
Mythriel Avatar asked Apr 05 '12 08:04

Mythriel


3 Answers

There are no hard and fast rules on how to structure your app using bundles, but here's what I came to after developing on Symfony2 for close to a year.

Use one app specific bundle. At first, I started with multiple bundles like CommonBundle, UserBundle, MainBundle, BlogBundle, ContactBundle, etc. That proved to be not so convenient in the end, so I switched to just one app specific bundle — AppBundle.

You can organize your code neatly using subnamespaces. For example, the backend controllers would go to the AppBundle\Controller\Backend subnamespace.

Note that I'm talking about one app specific bundle — that stuff that's unique to the concrete app and won't make sense to reuse elsewhere. You can still develop separate bundles for reusable stuff and put them into the vendors infrastructure.

Keep non Symfony specific stuff out of bundles. There is no need to have a bundle for the model and the Service Layer classes in a bundle if they are not Symfony2 specific. See this question and my answer for further details.

like image 78
Elnur Abdurrakhimov Avatar answered Oct 30 '22 03:10

Elnur Abdurrakhimov


Like Elnur said, use one AppBundle is a good practice.

A single bundle implements the MVC pattern himself so i think it's not a good idea to use bundles to separate your layers.

I think the best way to use bundles is to think "open source". If the feature you are developping is enough generic to be released for everyone, or to be reused in a future project, place this feature in a bundle. This way will force you to build the feature without any business rule which belong in your AppBundle.

Bundles are bricks

like image 26
Paul Andrieux Avatar answered Oct 30 '22 02:10

Paul Andrieux


There are different ways to organise application structure for your projects. But if you want to distribute your bundles and follow symfony best practices, then bundles are more features than separation of UI. More about bundles read in documentation.

I have two projects with the following structures, both valid I think:

  1. making a bundle for each feature: BlogBundle, StoreBundle and so on, and AppBundle that contains general stuff. No Backend/Frontend separation. It's SaaS where backend is frontend in most cases.
  2. One bundle for frontend, one for backend. They share only entities and domain specific stuff. The application has two different ends.
like image 2
meze Avatar answered Oct 30 '22 03:10

meze