How to import resources from a given controller directory structure in Symfony2

747 Views Asked by At

I'm working on an application that is growing rapidly which is causing more and more controllers over time and I'm always trying to maintain good practices for do not have as many lines per controller. Right now are almost 40 controllers in Controller directory and it's a bit complicated found one when need to add code or edit or something else so I'm thinking in order them inside subfolders under Controller directory as follow:

src\
    AppBundle\
        Controller\
            Comunes\
                CiudadController.php
                DuplicadosCedulaController.php
                ...
            RegistroUsuarios\
                EmpresaController.php
                NaturalController.php
                ...
            RPNI\
                CodigoArancelarioController.php
                RPNIProductoPaso1Controller.php
                ...
            BuscarEmpresaController.php
            DistribuidorController.php
            ...

But that reorder is causing this error on my application:

FileLoaderLoadException: Cannot import resource "/var/www/html/project.dev/src/AppBundle/Controller/" from "/var/www/html/projectdev/app/config/routing.yml". (Class AppBundle\Controller\EmpresaController does not exist)

Since apparently Symfony is not able to find the controller class when it's not on Controller directory. I've found this topic but is not clear to me what the problem is. I don't know if this is possible or not I read Controller Naming Pattern at Symfony docs but is not so helpful. Any advice around this? A workaround? Suggestions for a better project structure organization?

Note: I made only one bundle because has no sense more than one since the application will not works for separate bundles so following Syfmony Best Practices I come with only one bundle

Edit

This is weird and I don't know how all is working again, I've move all the controllers from Controller to subfolders inside that directory as my example above shows and didn't change nothing at all on routing.yml and Symfony keep getting controllers even if they are in subfolders: amazing!! Ahhh very important just remember CLEAR CACHE command, the most important Symfony command I believe, many of developers issues are cause of this, I forgot complete to clear it and test changes!!

3

There are 3 best solutions below

3
On BEST ANSWER

Here is a working example:

routing:

st_mainsiteweb_admin_subsite_create_template:
    path: /subsite/create-template
    defaults:
        _controller: STMainSiteWebBundle:Admin/SubSite:createTemplate

directory structure:

ST\
  MainSiteWebBundle\
             Controller\
                      Admin\
                         SubSiteController -> createTemplateAction

Is this are you looking for?

0
On

I never tried that so it's just a theoretical answer. If you want to have the controllers like this is perfectly fine, but then you should need to map them in the routing.

Controller\
        Comunes\
            CiudadController.php
            DuplicadosCedulaController.php

Then will be matched in routing yaml:

comunes:
    resource: "@yourBundle/Controller/Comunes"
    type: annotation

And so on for every different directory. As far as I know they are automatically loaded from Controller/ directory, but if you place them on any other place you need to reference them in the routing.

0
On

I do it 2h, now is ok, no routing, no other file.

Just change the namespace of

CiudadController.php
DuplicadosCedulaController.php
....

from AppBundle\Controller to AppBundle\Controller\Comunes