Search code examples
phpsymfonyroutesannotationsenvironment-variables

Environment variables in route annotations


I am making a website that handles subdomains, I have setup an environment variable called base_host and now I want to use this into the @route annotation in Symfony 4.

I have tried sofar: @Route("/", name="homepage", host="{afdeling}.{domain}", defaults={"domain"="%env(base_host)%"}, requirements={"domain"="%env(base_host)%"}) but this give me an error: using %env(base_host)% is not allowed in routing configuration

I have also tried: @Route("/", name="homepage", host="{afdeling}.%env(base_host)%") this did not work at all.

also what I have tried is: @Route("/", name="homepage", host="{afdeling}.{domain}", defaults={"domain"="%base_host%"}, requirements={"domain"="%base_host%"}) but this gives me the error message: The parameter "base_host" must be defined.

how can I achieve this?


Solution

  • Environment variables are not supported (yet) inside routes configuration. As workaround you can create a "proxy" container parameter to achieve it:

    # app/config/config.yml <= Sf 3.4 >= config/services.yaml
    parameters:
        base_host: '%env(BASE_HOST)%'
    

    Then use %base_host% for any route option.