我开发了一个控制器,用于使用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视图,因为它对调试非常有用,但是我想在应用程序投入生产时限制使用_format = html调用此控制器的可能性。如何确定是从开发环境还是从生产环境调用控制器?
答案 0 :(得分:19)
从服务容器中检索内核并使用内置方法:
$kernel = $this->get('kernel');
$kernel->isDebug(); // in most cases: false if env=prod, true if env=dev/test
$kernel->getEnvironment(); // prod, dev, test