我正试图看到一个小树枝扩展,但我挂在@安全。上下文注入,因为它返回给我一个错误,我在构造函数中添加参数?我是几个人在网上搜索,但问题是我找不到解决方案是我做错了什么?
目标是注入安全性。上下文来访问当前用户。
<?php
namespace Acme'AppBundle'Twig'Extension;
use Symfony'Component'Security'Core'SecurityContextInterface;
class reserveExtension extends 'Twig_Extension
{
protected $context;
public function __construct(SecurityContext $context)
{
$this->context = $context;
}
public function getFilters()
{
return array(new 'Twig_SimpleFilter('reserve_currentUser', array($this, 'reserveCurrentUser')));
}
function reserveCurrentUser($entity)
{
var_dump($this->context->getToken()->getUser());
}
public function getName()
{
return 'twig_extension_reserve';
}
}
服务定义:services:
reserveExtension:
class: Acme'AppBundle'Twig'Extension'reserveExtension
arguments: [@security.context]
tags:
- { name: twig.extension }
错误:Catchable Fatal Error: Argument 1 passed to Acme'AppBundle'Twig'Extension'ReserveExtension::__construct() must be an instance of Acme'AppBundle'Twig'Extension'SecurityContext, instance of Symfony'Component'Security'Core'SecurityContext given, called in D:'wamp'www'Acme'app'cache'dev'appDevDebugProjectContainer.php on line 432 and defined
您的use语句是错误的,您有use ...'SecurityContextInterface
,但您在实际代码中有SecurityContext
,更改其中一个