ReynierPM ReynierPM - 5 months ago 25
PHP Question

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

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!!

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?

Comments