检查是否在“开发"中带 Symfony 的控制器内的模式 [英] Check if in "dev" mode inside a Controller with Symfony

查看:25
本文介绍了检查是否在“开发"中带 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.

这篇关于检查是否在“开发"中带 Symfony 的控制器内的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆