宿主注释中的环境变量

Gio*_*and 5 php routing annotations environment-variables symfony

我正在制作一个处理子域的网站,我已经设置了一个名为base_host的环境变量,现在我想@route在Symfony 4 的注释中使用它。

我尝试过sofar:@Route("/", name="homepage", host="{afdeling}.{domain}", defaults={"domain"="%env(base_host)%"}, requirements={"domain"="%env(base_host)%"})但这给了我一个错误:using %env(base_host)% is not allowed in routing configuration

我也尝试过:@Route("/", name="homepage", host="{afdeling}.%env(base_host)%")这根本没有用。

我也尝试过的是:@Route("/", name="homepage", host="{afdeling}.{domain}", defaults={"domain"="%base_host%"}, requirements={"domain"="%base_host%"}) 但这给了我错误信息:The parameter "base_host" must be defined.

我怎样才能做到这一点?

yce*_*uto 6

路由配置内部尚不支持环境变量。解决方法是,可以创建一个“ proxy ”容器参数来实现它:

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

然后%base_host%用于任何路由选项。

  • 今天仍然有效! (2认同)