Symfony 4 global route prefix

super.t picture super.t · Jan 5, 2018 · Viewed 9.1k times · Source

I can't find any info about the global route prefix in a Symfony 4 application. The only thing I've found is annotating the controllers with @route. But I don't use annotations and I need all the controllers to have the same prefix.

Now I could do that in S3 like this in the app/config/routing.yml file:

app:
    resource: '@AppBundle/Controller/'
    type: annotation
    prefix: /foo

But S4 is bundleless and I can't do the same - I would have to create a bundle which I don't need.

Is it possible to define a global route prefix in Symfony 4 at all or I'm going to end up with prefixing every single root/creating a custom route loader, especially if I configure routes in YAML?

Answer

Arleigh Hix picture Arleigh Hix · Feb 24, 2018

This works with annotations, giving every controller 'this/prefix' :

# file: config/routes/annotations.yaml

controllers:
    resource: ../../src/Controller/
    type: annotation
    prefix: this/prefix

Then for imported routes 'this/prefix' gets applied to all of FOSUserBundle's routes below (yes I tested it).

# file: config/routes.yaml

fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"
    prefix: this/prefix

If you are not using annotations you should be able to import your separate routing file (which can import many more) applying the prefix to everything in the resource. But drop the "@" notation and use relative path to your file. The value of 'this/prefix' can be configured as a Service Container Parameter, look here: http://symfony.com/doc/current/routing/service_container_parameters.html