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

I am working on a fast growing application that calls more and more controllers over time, and I always try to maintain good practices as they don't have many lines per controller. There Controller

are almost 40 controllers in the directory now and it's a bit tricky to find when code needs to be added or changed or whatever, so I think they are inside subfolders in the directory Controller

like this:

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

      

But this change is causing this error in my application:

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

Because apparently Symfony cannot find the controller class if it is not in the directory Controller

. I found this thread, but it is not clear to me what the problem is. I don't know if this is possible or not. I read the controller naming pattern in the symfony docs, but it's not that helpful. Any tips on this? Workaround? Suggestions for better organization of the project structure?

Note. I only made one package because it doesn't make sense for more than one as the app won't work for individual packages, so follow the Syfmony Best Practices I only come with one package

Edit

It's weird and I don't know how things work again, I moved all controllers from Controller

to subfolders inside that directory as shown in my example above and didn't change anything to routing.yml

and Symfony keeps getting controllers even if they are in subfolders: amazing! Ahhh it's very important to just remember the CLEAR CACHE command , the most important Symfony command, I guess many of the developer problems are causing this, I forgot to complete the clean and check the changes!

+3


source to share


3 answers


Here's 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

      

Are you looking for this?

+1


source


I have been doing this for 2 hours, now everything is fine, no routing, no other file.

Just change the namespace



CiudadController.php
DuplicadosCedulaController.php
....

      

from AppBundle \ Controller to AppBundle \ Controller \ Comunes

+1


source


I've never tried it, so this is just a theoretical answer. If you want controllers like this to be fine, but then you will need to map them in your routing .

Controller\
        Comunes\
            CiudadController.php
            DuplicadosCedulaController.php

      

Then will be mapped in the yaml routing:

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

      

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

0


source







All Articles