7

In my application, I use Symfony 4. I want Symfony to search for controllers in two directories: A and B. I found something like this:

controllers:
    resource: '../src/DirectoryA/Controller/'
    type:     annotation

, but it only works for one directory. How can I have Symfony to search for controllers in two directories?

Regards

mariusz08
  • 83
  • 1
  • 4

2 Answers2

15

In your config/services.yaml

App\DirectoryA\Controller\: # assuming you have namespace like that
    resource: '../src/DirectoryA/Controller'
    tags: ['controller.service_arguments']
App\DirectoryB\Controller\: # assuming you have namespace like that
    resource: '../src/DirectoryB/Controller'
    tags: ['controller.service_arguments']

This will add next directory for service arguments. Thats answers your questions based In directory, what you have posted is routing file, in there would be similiar

controllers_a:
    resource: '../src/DirectoryA/Controller/'
    type:     annotation
controllers_b:
    resource: '../src/DirectoryB/Controller/'
    type:     annotation
M. Kebza
  • 1,488
  • 1
  • 10
  • 14
11

The accepted answer is of course completely correct.

However, once you move from having one controller directory to multiple directories, updating your services.yaml file can be a bit of a pain. Even having to have directories specifically for controllers can be limiting.

Here is an alternate approach which allows creating controllers wherever you want and automatically tagging them.

Start with an empty controller interface for tagging.

interface ControllerInterface {}

Now have all your controllers implement the interface

class Controller1 implements ControllerInterface { ...
class Controller2 implements ControllerInterface { ...

And then adjust the kernel to automatically tag all your controller interface classes with the controller tag.

# src/Kernel.php
protected function build(ContainerBuilder $container)
{
    $container->registerForAutoconfiguration(ControllerInterface::class)
        ->addTag('controller.service_arguments')
    ;
}

And presto. You can create your controllers wherever you want with nothing in services.yaml.

Update: If you would like to avoid editing Kernel.php then you can use the _instanceof functionality in your services.yaml file.

#config/services.yaml
services:
    _instanceof:
        App\Contract\ControllerInterface:
            tags: ['controller.service_arguments']

Another Update: As long as your controller extends Symfony's AbstractController then no additional tagging is needed. You can even delete the default controller lines in the default services.yaml file if you want.

Cerad
  • 48,157
  • 8
  • 90
  • 92
  • 3
    Thats what I was lookign for. I need somehow to tag controllers because I have the controller separated by subdomains... But following that, now my route on controllers (annotation) has somehow lost. Route not found...any idea? Thanks! – Albeis Aug 05 '19 at 14:22
  • Just I had to add new alias on annotations.yml for the new controllers path*. Thanks! – Albeis Aug 05 '19 at 14:41
  • 1
    Glad to be of help. Especially when I do nothing. By the way, while I generally prefer to edit the Kernel.php file, you can also use the [_instanceof](https://symfony.com/doc/current/service_container/tags.html#autoconfiguring-tags) parameter to tag your controllers in the services.yaml file. – Cerad Aug 05 '19 at 14:56
  • @ClaudioFerraro Yep. Works fine in S5. – Cerad Nov 16 '20 at 14:01