3

I'd like to build a mid-sized website which also includes a mobile website and an API. As I already have some experience with Symfony I'd like to build the project on top of it.

However, I have no idea how to structure my project. Maybe there are some guidelines how to approach this problem?

Is it a good idea to build a each component as an individual Bundle? E.g. CoreBundle, APIBundle, WebsiteBundle and MobileWebsiteBundle, whereas CoreBundle would contain all Models and Validation and the other Bundles Controllers and Views?

Or is there a completely other way?

XM2
  • 31
  • 1

1 Answers1

2

You don't need to put all of your code into the bundles. In fact the more code you put there, the more you're coupling it to the framework.

Consider extracting some of the non-Symfony related code into separate namespaces and treat bundles more like a glue between your PHP code and Symfony framework.

For example:

  • Acme
    • Bundles
      • ApiBundle
      • WebsiteBundle
    • Entity
    • Tools
    • Twig

If you decide to put entities outside of the bundle you'll have to configure a new mapping in the app/config/config.yml:

doctrine:
    orm:
        mappings:
            Bossa:
                type:       annotation
                is_bundle:  false
                dir:        %kernel.root_dir%/../src/Acme/Entity
                prefix:     Acme\Entity\
                alias:      Acme
Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125
  • The advantage of putting code inside bundles is many tools/bundles like Assetic, Doctrine use pre-configured bundle directories (`/Entity`, `/Resources/views`) to find/load resources. It is always possible to make these tools load resources outside the bundle space but it take extra configuration. – noisebleed Dec 30 '12 at 16:29
  • Configuring ORM mappings for entities is simple. Also, I'm not advising on putting everything outside of the bundle. Extracting views doesn't make much sense (but twig extensions makes sense if they're re-usable). – Jakub Zalas Dec 30 '12 at 21:06
  • What do you think about create another one kernel for API named ApiKernel and how about already existing bundles? For instance, I store all bundles in src/VendorName/ and it related to PSR-0, but I want to move them into the directory named `Bundles` as you show above. I think it is interesting way. – Igor Timoshenko Dec 30 '12 at 21:24
  • Moving bundles to a Bundle namespace shouldn't be hard (you'll have to fix namespaces and bundle names). Only create an ApiKernel if you need the separation. That would depend on a project, sounds good in your case. You'd also need separate front controllers and configurations. – Jakub Zalas Dec 30 '12 at 22:29