Reputation: 1
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?
Upvotes: 0
Views: 2046
Reputation: 41934
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
Upvotes: 1
Reputation: 35169
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.
Upvotes: 1