3

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 Answers 3

1

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?

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not test your solution and perhaps it works but since my code start working again I'll give points to you, take a look to my edition
@ReynierPM I've seen this technique in Sylius source code since I'm using it.. But your solution is weird, double check if it's working correctly even it's working now..
Already checked I'm working on it now, adding things and much more, I test and re-test it but don't worry I'll come back here if any mistake is present
1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.