检查是否在“开发"中带 Symfony 的控制器内的模式

在 Symfony2.x 应用程序中使用开发模式时,通常在语言环境中工作.因此,此类功能无法按预期工作(例如,尝试获取 localhost 下的当前 IP).这可能是一个问题,例如当您尝试使用这种基于 ip 的网络服务时.因此,我只想知道如何在控制器内部检查 Symfony2 应用程序是否在 dev 模式下运行.这样就可以根据模式设置控制器的行为.

When using dev mode with a Symfony2.x application, one usually works in locale. Hence, such function does not works as expected (for instance, try to get the current IP under localhost). This could be a problem, e.g. when one try to use such ip-based web service. Hence, I just want to know how to check inside a controller if the Symfony2 application is running in dev mode or not. In that way one can set the behavior of the controller depending by the mode.

有什么想法吗?

推荐答案

要在 Controller 中获取当前环境,您可以使用:

To get the current environment in a Controller you can use:

$this->container->getParameter('kernel.environment');

所以你只要把它放在一个 if() 语句中来检查它是否等于 dev.

So you just put that in an if() statement to check if it equals to dev.

相关文章