symfony2.8 응용 프로그램에서 작업 중이며 exceptionController의 showAction을 재정의하려고했으나 현재 로그인 한 사용자를 얻을 수 없다는 점을 제외하고는 모두 정상적으로 동작합니다. security.token_storage는 항상 null을 반환합니다. . security.yaml :Symfony : Custom 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 }
과 설정에 내가 서비스 블록에서 서비스로 컨트롤러를 선언
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
)
));
}
}
어떤 제안, 제발 여기 twig.controller.exception: class: %twig.controller.exception.class% arguments: ["@twig",%kernel.debug%,"@security.token_storage"]
및 내 컨트롤러입니다!
관련 코드를 추가하십시오. –
showAction 내에서 $ this-> tokenStorage-> getToken();을 호출합니다. 그것은 null을 돌려 준다. tokenstorage는 서비스 자체 인 컨트롤러 내부에있는 injotced 서비스이다. – ZeSoft
@ZeSoft : 아니, 그는 당신의 질문을 실제 코드로 업데이트하는 것이었다. 또한,'security.xml'을 포함 시키십시오 - 이것이 작동하지 않는 이유를 우리에게 알려 주어야합니다. –