Symfony2 conceptual issue: general bundles vs. specific ones
Asked Answered
A

2

37

Edit: the Symfony best practices answer most of my questions.

I have several questions concerning my Symfony2 app.

It will have a frontend and a backend, and they will use some common code (such as a date displayer, paginator, some templates often used, etc).

So, I created one FrontendBundle and one BackendBundle, each containing for example their respective layout. First question: is that good practice to create bundles for frontend and backend, which are "general" bundles which won't even have a controller?

Second question: I read on a cookbook that I should not put my layouts in bundles, but in the app/Resources/views/ directory. I already have a base.html.twig file in it, and I wonder if I should put my layouts in there too, like a frontend_layout.html.twig file?

I created a bundle named RootBundle, which would contain everything my app needs in frontend AND backend. Is that a good practice or not? Or I should create a dedicated bundle for each functionality proposed, such as a PaginatorBundle, a DateDisplayerBundle, etc? It sounds weird that I have one "miscellaneous" bundle containing everything I don't know where to put. How do you do that?

Agglutinative answered 4/11, 2011 at 16:1 Comment(3)
@elnur why do you add the sf2.1 tag?Doi
Because it's related to Symfony 2.1 as much as to Symfony 2.0. Until the Symfony tags mess is fixed, I think it's best to keep it like this.Pronate
I agree with that, the problem is not Symfony2.0-specificAgglutinative
P
80

The new approach

After several months since I wrote this answer, my approach has changed, so I'm sharing it with the community. This answer is still pretty popular and can lead newcomers to the approach I don't think is the best one anymore. So...

Now I have only one app specific bundle and I call it AppBundle. There were several problems with the old approach and here are some of them:

  • Creating a lot of bundles is tedious. You have to create a bundle class and a bunch of standard folders for each new bundle and then activate it and register its routes and DI and whatnot.

  • Unnecessary hardcore decision making process. Sometimes you just can't decide which bundle a particular thing belongs to because it's used by more than one bundle. And after you spend a half a day and finally make your hard decision on where to put it, you'll find that in a couple of days or weeks you won't be able to tell right away which bundle to look that thing in — because most of the times the decision wasn't based on pure logic and you had to choose based on a coin toss or whatever means you use to bring higher powers for help.

    I suggested using CommonBundle for common stuff in the past but doing that you'll have to do a lot of unnecessary refactorings moving a thing to and from CommonBundle based on how many or few bundles will use that thing later.

  • App specific bundles are interdependent anyway. When people meet the idea of bundles for the first time, one of the main thought that goes through their minds is something like “Yay! I'll have me a bunch of reusable bundles!” That idea is great and I have nothing against it; the problem is that app specific bundles are not that reusable anyway — there are interdependent. Forget about reuse in this case.

  • No idea where to put Behat features and step definitions. This problem is related to the previous ones: you have to repeat the same brainless motions for each bundle and then make hardcore decisions.

    When I started writing Behat features, I just couldn't decide where to put a lot of features and step definitions because they belonged to several bundles at a time. Putting them into CommonBundle seemed to be even worse, because that's the last bundle I would look for that stuff in. So, I ended up creating FeatureBundle for that.

Switching to a single bundle solved all these problems.

I've also seen some people having a separate bundle for, say, all the entities. I don't like this approach neither and actually suggest keeping entities and other non Symfony2 specific stuff out of the bundles.

Note again that this new approach applies to app specific bundles. Official docs and other places are full of great advice on how to structure bundles intended to be shared with others and reused across numerous projects. I write bundles of this type as well. But what I've found out after months of working on Symfony2 projects is that there is a difference between the bundles intended for reuse and the app specific ones — one approach doesn't fit all.

And, of course, when you see something reusable emerging in your app specific bundle, just extract it, put it in a separate repo and install as a vendor.

Also I've found myself using subnamespaces much more actively as a way to partition the bundle logically — instead of creating a bunch of bundles for that and going through all those troubles.

The old approach

There are no hard and fast rules or silver bullets, but I'll share my approach of doing things — maybe it will give you an insight or two.

First of all, I don't have two all-encompassing bundles like FrontendBundle and BackendBundle. Instead, my bundles have both frontend and backend controllers, views, etc. So, if I strip everything from my UserBundle except for controllers and views, its structure would look like this:

UserBundle
├── Controller
│   ├── Admin
│   │   └── UserController.php
│   └── UserController.php
├── Resources
│   └── views
│       ├── Admin
│       │   └── User
│       │       ├── add.html.twig
│       │       ├── delete.html.twig
│       │       ├── edit.html.twig
│       │       ├── form.html.twig
│       │       └── index.html.twig
│       └── User
│           ├── edit.html.twig
│           ├── sign-in.html.twig
│           ├── sign-up.html.twig
│           └── view.html.twig
└── UserBundle.php

Second, I have CommonBundle which I use for stuff shared by several bundles:

CommonBundle
├── Resources
│   ├── public
│   │   ├── css
│   │   │   ├── admin.css
│   │   │   ├── common.css
│   │   │   └── public.css
│   │   └── img
│   │       ├── add.png
│   │       ├── delete.png
│   │       ├── edit.png
│   │       ├── error.png
│   │       ├── return.png
│   │       ├── success.png
│   │       └── upload.png
│   └── views
│       ├── Admin
│       │   └── layout.html.twig
│       └── layout.html.twig
└── CommonBundle.php

My app/Resources/views/base.html.twig is almost the same as it comes with Symfony Standard distribution:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>{{ block('title') | striptags | raw }}</title>
        {% block stylesheets %}{% endblock %}
    </head>
    <body>
        {% block body %}{% endblock %}
        {% block javascripts %}{% endblock %}
    </body>
</html>

Both CommonBundle/Resources/views/layout.html and CommonBundle/Resources/views/Admin/layout.html extend app/Resources/views/base.html.twig. Other bundles' templates extend one of these two layouts, depending on whether they are for frontend or backend. Basically, this is how I'm using the Three-level Inheritance approach.

So, I'd put your date displayer into CommonBundle. Depending on its complexity it could be just a template, a macro or a Twig extension.

Pagination is a common problem, so I suggest you to use one of the existing bundles instead of reinventing the wheel — if they suite your needs, of course.

And yes, it's perfectly okay to have bundles without controllers or views, etc.

Pronate answered 4/11, 2011 at 23:23 Comment(4)
I think another good name for CommonBundle could be <insert your project's name here>Bundle, because it should contain all the stuff that is specific to your project and that you don't think you're ever going to reuse anywhere.Lauraine
I like your approach very much, but I prefer to have different bundles for admin and main (to have different controllers and views in different bundles). The main reason for this is that admin part is often dependent on main (services, entities, etc), but main is not dependent on admin part. Less code in main bundle :)Deeannadeeanne
After reading your updated approach I have no idea if the upvotes are for the old or the new one :DHandbreadth
Now even part of the Symfony best practises : symfony.com/doc/master/best_practices/…Radarman
R
4

I suggest creating a DateDisplayerBundle and a PaginatorBundle instead of putting their related code in a more general bundle. There are a few reasons for this:

  • The role of each bundle is very clear, and you know where your code is.
  • Sharing pieces of functionality (date displayer, paginator) between different projects is simpler when you have separate bundles, versus one general bundle that you then may need to prune.

There's no hard rule saying that bundles must have controllers. Bundles can have any mix of business logic, templates, controllers, and configuration, but there's no restriction on what you can store in them.

On the other hand, if your functionality is not very complex, it may not warrant being contained within a bundle at all. In this case you could create a library in /vendor for it. Symfony makes use of a number of libraries in this fashion (see Monolog and Doctrine for example.)

As for your second question, I think the reason for keeping layouts in app\Resources\views is because it's a convenient way for you to keep track of all your layouts. When you have a project that has many bundles, you might lose track of where a certain layout is. But if you keep them all within one centralized location, you'll always know exactly where to look. As with many things in Symfony2, this isn't a rule that's set in stone. You could easily store your layouts in a bundle, but I don't think it's the recommended practice.

As for your question about your general Root bundle, I would say in most cases you should avoid shoehorning a bunch of different features in one bundle. See my earlier points about keeping your bundles specific. When I started developing with Symfony2, I had some trouble determining what code should go in what bundle. It's not how I was used to thinking about programming. But eventually you start to see how the individual pieces of the puzzle fit, and that makes determining the bundle structure easier.

Rosefish answered 4/11, 2011 at 18:17 Comment(2)
Some of your points are good, but I strongly disagree with your suggestion of putting something into the vendor folder. This folder should be ignored in your repo and it's being created and filled with bin/vendors script. Details are here.Pronate
@elnur I find the vendor/ folder to be very useful. Sometimes I create my own reusable libraries there, upload to my VCS host of choice (bitbucket), and have them available on any project I need them through composer. Edit: and just now I notice how old this thread is.Marillin

© 2022 - 2024 — McMap. All rights reserved.