2012-09-28 11 views
8

ho sviluppato un controller per rispondere alle petizioni AJAX utilizzando JSON:Symfony2: determinare se un controller è chiamato da un ambiente di sviluppo o da un ambiente di produzione

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)); 

} 

ho attivato la visualizzazione HTML, come è molto utile per il debug, ma mi piacerebbe limitare la possibilità di chiamare questo controller con _format = html mentre l'app è in produzione. Come posso determinare se un controller viene chiamato da un ambiente di sviluppo o da un ambiente di produzione?

+0

http://stackoverflow.com/questions/8272090/get-environment-inside-controller – moonwave99

risposta

19

Recupera il kernel dal contenitore di servizio e utilizzare il costruito nel metodi:

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