1

I'm am trying to organize my controllers a bit more in a Symfony 5.3 application. They are in a couple of subdirectories and use annotations for their routing. Here are some samples:

  • ../src/StuffA/Controller/ControllerA.php
  • ../src/StuffB/Controller/ControllerB.php

I now want to specify both inside my annotations.yaml. I tried several approaches, but could not find anything that works:

Wildcards - Does not work

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

Multiple Paths - Does not work

controllers:
    resource: '../../src/{StuffA,StuffB}/Controller'
    type: annotation

Single Paths - Works but only for one Controller

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

Any hint on what I'm doing wrong?

ToBe
  • 2,667
  • 1
  • 18
  • 30

1 Answers1

3

Just create multiple resource keys.

E.g.:

# annotations.yaml
controllersFoo:
    resource: ../../src/Foo/Controller/
    type: annotation

controllersBar:
    resource: ../../src/Bar/Controller/
    type: annotation

I don't think you can use wildcards or glob patterns with the annotation loader.

yivi
  • 42,438
  • 18
  • 116
  • 138
  • I will probably end up having up to 10 such folders. Any other ideas on how to make it work without specifying each folder individually? – ToBe Oct 02 '21 at 08:07
  • 2
    Having 10 keys does not seem to be a big issue, IMO. I don’t use annotations/attributes, so my configuration for everything is organized the way I want, separated from the code. – yivi Oct 02 '21 at 10:08
  • 1
    Just to sort of butt in here, I second the notion of using a routing file instead of annotations when you decide to move controllers elsewhere. I use single action controllers and place those files wherever it makes most sense to me. No controller directories. I once pitched the idea of using autoconfigure to load routes from controller files but it went nowhere. – Cerad Oct 02 '21 at 13:17
  • Ok, I'll use multiple paths in annotations for now. Might switch over to routing.yml. I just likes to specify routing inside the controllers to have everything at the same place. I'll keep a look out for nice solutions inside routing.yml however. – ToBe Oct 04 '21 at 08:18