2016-11-06 7 views
0

symfony2.8アプリケーションで動作していて、exceptionControllerのshowActionをオーバーライドしようとしていますが、現在ログインしているユーザーを取得できないことを除いてすべて正常に動作します。security.token_storageは常にnullを返します。 。 security.yaml:symfony:カスタムExceptionController

security: 
    encoders: 
     FOS\UserBundle\Model\UserInterface: bcrypt 
role_hierarchy: 
    ROLE_ADMIN:  ROLE_USER 
    ROLE_SUPER_ADMIN: ROLE_ADMIN 
providers: 
    fos_userbundle: 
     id: fos_user.user_provider.username_email 
firewalls: 
    dev: 
     pattern: ^/(_(profiler|wdt)|css|images|js)/ 
     security: false 
    main: 
     pattern: ^/ 
     form_login: 
      provider: fos_userbundle 
      csrf_provider: security.csrf.token_manager 
      check_path: fos_user_security_check 
      login_path: fos_user_security_login 
      default_target_path: default_security_target 
      use_referer: true 
     logout: 
      path: fos_user_security_logout 
      target: default_security_target   
     anonymous: true 
     remember_me: 
      key: "%secret%" 
      lifetime: 604800 
      path:/
access_control: 
    - { path: ^/login, role: IS_AUTHENTICATED_ANONYMOUSLY } 
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } 
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } 
    - { path: ^/admin, role: ROLE_ADMIN } 

と私はサービスブロックの下でサービスとしてのコントローラを宣言configに:twig.controller.exception: class: %twig.controller.exception.class% arguments: ["@twig",%kernel.debug%,"@security.token_storage"] 、ここでは私のコントローラである:

namespace test\testBundle\Controller; 
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; 
use Symfony\Component\HttpKernel\Exception\FlattenException; 
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as baseController; 
use FOS\UserBundle\Model\UserInterface; 

class ExceptionController extends baseController 
{ 
    private $tokenStorage; 

public function __construct(\Twig_Environment $twig,$debug,TokenStorage $tokenStorage) 
{ 
    parent::__construct($twig,$debug); 
    $this->tokenStorage= $tokenStorage; 
} 
/** 
* Converts an Exception to a Response. 
* 
* @param Request    $request The request 
* @param FlattenException  $exception A FlattenException instance 
* @param DebugLoggerInterface $logger A DebugLoggerInterface instance 
* @param string    $_format The format to use for rendering (html, xml, ...) 
* 
* @return Response 
* 
* @throws \InvalidArgumentException When the exception template does not exist 
*/ 
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null, $_format = 'html') 
{ 
    /*** $this->securityContext is a pitfall never use it here recommended by the documentation 
     $user = $this->tokenStorage->getToken()->getUser();//$this->tokenStorage->getToken() is always null 

    if (!is_object($user) || !$user instanceof UserInterface) { 
     $user=null; 
    } 

    $currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1)); 
    $code = $exception->getStatusCode(); 
     return new Response($this->twig->render(
      $this->findTemplate($request, $_format, $code, $this->debug), 
      array(
       'status_code' => $code, 
       'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '', 
       'exception'  => $exception, 
       'logger'   => $logger, 
       'currentContent' => $currentContent 
      ) 
     )); 
} 
} 

任意の提案は、してください!

+2

関連コードを追加してください。 showAction内の –

+0

は$ this-> tokenStorage-> getToken()を呼び出します。それはヌルを返します。トークンストアはサービス自体のコントローラーの内部に置かれています – ZeSoft

+0

@ZeSoft:いいえ、あなたの質問は実際のコードで更新されました。また、あなたの 'security.xml'を含めてください - なぜこれがうまくいかないのかのヒントを与えるべきです –

答えて

1

私は元のExceptionControllerクラスとあなたのクラスを比較しましたが、ごくわずかの違いがあります。tokenStorageの周りにあります。このように、私はにハッキングの多くの点を見ていないだけで、例外を傍受してResponseを提供しています。

この目的のために、KERNEL_EXCEPTIONというイベントコールがあり、それを聞くことができ、必要に応じてResponseを自分でスワップできます。ページEvents and Event Listenersはどのように記述します。

だから、基本的には:

  1. タグを独自のサービスを作成し、TokenStorage
  2. を注入して、あなたのサービスでkernel.exception
  3. と、あなたが必要な場合は、やると入れたいかを決める:

    $event->setResponse($myNewResponse); 
    

これはすべきことです。これが役立つことを願って...

+0

接続されたユーザーの情報を使用していないコードがあるので、tokenstorageが注入されます。私はすでにonkernelExceptionを使用していますが、ユーザーがNULLに設定されていても、優先順位を255に設定しても、コードはトークンストレージの設定後に実行されますが、何も動作していないようです – ZeSoft

関連する問題