我试图看到做一个twig扩展,但我挂在@ security.context注入,因为它给我一个错误,我在构造函数中添加参数?我有几个人在互联网上搜索,但问题是我找不到解决方案是我做错了什么?
目标是注入security.context以访问当前用户。
<?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
谢谢!
答案 0 :(得分:1)
您的使用声明有误,您有use ...\SecurityContextInterface
,但实际代码中有SecurityContext
,请更改其中一个。