Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

symfony

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?

like image 234
DaveFX Avatar asked Sep 28 '12 10:09

DaveFX


1 Answers

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
like image 51
AdrienBrault Avatar answered Nov 11 '22 16:11

AdrienBrault