0

I want to separate my sections in Symfony by folders like this:

src
 - Blog
  -- Controller
  -- Entity
  -- Form
  -- Repository
 - Main
  -- Controller
  -- Entity
  -- Form
  -- Repository

With bundle I can't use annotation in controller and entity and I can't use make:entity in my blog separately. Is there any way to create this structure in symfony 4?

theduck
  • 2,589
  • 13
  • 17
  • 23
jynx maze
  • 1
  • 1
  • 1
    since it's probably mostly about classes, adapting composer.json and the config/services.yaml should be sufficient – Jakumi Oct 19 '19 at 09:52
  • 1
    Back in the good ole days of Symfony 2 we used to use bundles for this sort of stuff. BlogBundle, MainBundle etc. You still can but there are good reasons bundles have pretty much gone away at the application level. If you want a different layout then use whatever "make directory" command your operating system supports and start tweaking. I suspect you might find that it's not really worth the effort. – Cerad Oct 19 '19 at 14:11
  • Are you asking how to create folders manually? If so, please delete your question since this is not the site for that. If you are trying to do it with code, please explain that and show some code that you have tried. :) – Rojo Oct 19 '19 at 14:34

2 Answers2

1

As Symfony is a PHP framework and doesn't impose any choices, you're free to place your classes anywhere you like as long as the Composer autoloader is able to load them. You might have to configure packages to look for objects in other namespaces/places as well. They are by default configured in your application to be loaded from the predefined file structure.

For instance, when your entity lives in src/Blog/Entity/Post, it should probably have the fully-qualified class name like App\Blog\Entity\Post. You then have to tweak the default Doctrine configuration to load entities with such namespace:

doctrine:
    # ...
    orm:
        # ...
        mappings:
            Blog:
                is_bundle: false
                type: annotation

                # before:
#               dir: '%kernel.project_dir%/src/Entity'
#               prefix: 'App\Entity'
#               alias: App

                # after:
                dir: '%kernel.project_dir%/src/Blog/Entity'
                prefix: 'App\Blog\Entity'
                alias: Blog

Another example, you have to change the routing configuration so that it looks for controllers in App\Blog\Controller:

blog_routes:
    resource: '../src/Blog/Controller/'
    type:     annotation
Wouter J
  • 41,455
  • 15
  • 107
  • 112
1

It's entirely possible, you just end up with slightly more complex configurations:

# services for all src/*/ directories, ecxluding 
App\:
    resource: '../src/*'
    exclude: 
      - ../src/Tests/
      - ../src/Kernel.php
      - '../src/*/{Entity,Migrations}'

App\Blog\Controller\:
    resource: '../src/Blog/Controller'
    tags: ['controller.service_arguments']
# repeat for other sub-directories
# App\Main\Controller\:

To use the Maker Bundle, give it a more specific path

# config/packages/dev/maker.yaml
# create this file if you need to configure anything
maker:
    # tell MakerBundle that all of your classes lives in an
    # Acme namespace, instead of the default App
    # (e.g. Acme\Entity\Article, Acme\Command\MyCommand, etc)
    root_namespace: 'App\Blog\'

Since it would all still be the same app, you could avoid the 'fake bundle' layout, by inverting it, and grouping each type of class by sections:

src/
  - Controller/
    -- Blog/
    -- Main/ 
  - Entity
    -- Blog/
    -- Main/ 

This would not need any configuration changes to the standard style.

Alister Bulman
  • 34,482
  • 9
  • 71
  • 110