php - Symfony2 : determine if a controller is called from a development environment or from a production environment

标签 php symfony

我开发了一个 Controller 来使用 JSON 响应 AJAX 请求:

class PeopleController extends Controller
{
public function listAction()
{
    $request = $this->getRequest();

    // if ajax only is going to be used uncomment next lines
    //if (!$request->isXmlHttpRequest())
    //throw $this->createNotFoundException('The page is not found');

    $repository = $this->getDoctrine()->getRepository('PeopleManagerBundle:People');
    $items = $repository->findAll();

    // yes, here we are retrieving "_format" from routing. In our case it's json
    $format = $request->getRequestFormat();

    return $this->render('::base.'.$format.'.twig', array('data' => $items));

}

我已启用 HTML View ,因为它对调试非常有用,但我想限制在应用程序处于生产状态时使用 _format=html 调用此 Controller 的可能性。如何确定 Controller 是从开发环境还是从生产环境调用的?

最佳答案

从服务容器中检索内核并使用内置方法:

$kernel = $this->get('kernel');
$kernel->isDebug(); // in most cases: false if env=prod, true if env=dev/test
$kernel->getEnvironment(); // prod, dev, test

关于php - Symfony2 : determine if a controller is called from a development environment or from a production environment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12638682/

相关文章:

php - 学说 2 : Saving Entity in Complex Relationship

php - 从表 B 中列出的相同 id 从 mysql 表 A 获取相关用户名

php - 在自定义主题 wordpress 上添加下拉菜单

php - 用户手册(seo + 自动生成)

symfony - 如何从 symfony2 Controller 中的parameters.yml 读取?

php - Symfony 路由正则表达式 : How to write an optional character?

php - ManyToMany Doctrine 与 FOSUserBundle 的关系

symfony - Doctrine2 多对一双向关系不起作用

php - 在 Symfony2 配置 TreeBuilder 中使用正则表达式验证?

php - 'user' 中的未知列 'where clause'