2014-03-02 22 views
0

在我的Symfony2應用程序中,當頁面未找到(404)時出現錯誤,並且我嘗試在我的onKernelRequest事件類中使用isGranted方法。Overriden onKernelRequest事件

所以問題是:如何檢查頁面是否在事件類中找到?

//編輯

一些代碼:

public function onKernelRequest(GetResponseEvent $event) { 
     $session = $event->getRequest()->getSession(); 
     $securityContext = $this->serviceContainer->get('security.context'); 

     /* The line below makes the script throw the following error: 
Fatal error: Uncaught exception 'Symfony\Component\Routing\Exception\ResourceNotFoundException' in C:\xampp................ in C:\xampp\....\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\EventListener\RouterListener.php on line 144 
     */ 
     if ($securityContext->isGranted('IS_AUTHENTICATED_FULLY')) { 
      // ... 
     } else { 
      // ... 
     } 
    } 
+0

你是怎麼定製錯誤的水電站? – Darmen

+0

通過將error.html.twig和error404.html.twig文件放在app/Resources/TwigBundle/views/Exception –

+0

我已經更新了這個問題。請重新閱讀 –

回答

1

,則不能使用is_granted因爲路由器上運行防火牆 看到這個comment

執行isGranted之前就檢查令牌之前:

if (null !== $securityContext->getToken() && $securityContext->isGranted('IS_AUTHENTICATED_FULLY')) { 
    // ... 
} else { 
    // ... 
} 
+0

我不想對異常做任何事情。如果沒有上面的事件類,它可以完美地工作。我想知道爲什麼上面的行是拋出一個錯誤。 –

+0

我更新了我的評論:) –

+0

我明白了。你有什麼想法可以用isGranted()方法執行我的操作嗎? –

相關問題