2012-05-07 54 views
15

我想知道是否在偵聽器中使用app_dev.php(在config.yml中設置爲服務)。它在getEnvironment()失敗,雖然這是我在控制器中使用的方法。Symfony2在迴應監聽器中獲取當前環境

public function onKernelResponse(FilterResponseEvent $event) { 
    $kernel = $event->getKernel(); 
    $this->isAppDev = ($kernel->getEnvironment() == "dev") ? true : false; 
} 

編輯解決方案: 在我的監聽器類:

use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; 
use Symfony\Component\HttpKernel\Kernel; 
private $kernel; 
public function __construct(Kernel $kernel) { 
    $this->kernel = $kernel 
} 
public function onKernelResponse(FilterResponseEvent $event) { 
    $this->isAppDev = ($this->kernel->getEnvironment() == "dev") ? true : false; 
} 

在config.yml的底部:

services: 
    my.listener: 
     class: Path\To\Listener 
     tags: 
      - { name: kernel.event_listener, event: kernel.response } 
     arguments: [@kernel] 

回答

相關問題