检查是否在“ dev”目录中Symfony在控制器内部运行模式 [英] Check if in "dev" mode inside a Controller with Symfony

查看:68
本文介绍了检查是否在“ dev”目录中Symfony在控制器内部运行模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Symfony2.x 应用程序中使用开发模式时,通常可以在区域设置中使用。因此,该功能无法按预期运行(例如,尝试在localhost下获取当前IP)。这可能是个问题,例如当人们尝试使用这种基于IP的Web服务时。因此,我只想知道如何在控制器内部检查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.

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

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