我开发了一个 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/