I've developed a controller for answering to AJAX petitions using JSON:
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));
}
I've enabled the HTML view, as it is very useful for debugging, but I'd like to limit the possibility of calling this controller with _format=html while the app is in production. How can I determine if a controller is called from a development environment or from a production environment?
Retrieve the kernel from the service container and use the built in methods:
$kernel = $this->get('kernel');
$kernel->isDebug(); // in most cases: false if env=prod, true if env=dev/test
$kernel->getEnvironment(); // prod, dev, test
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With