树枝扩展中的Symfony依赖注入 [英] Symfony dependency injection in twig extension
问题描述
好的,我试图创建依赖于其他服务(security.context)的树枝扩展,但遇到了一些麻烦。因此,这是我的服务声明:
Ok, I was trying to create twig extension with dependencies on other service (security.context) and got some troubles. So, here is my service declaration:
acme.twig.user_extension:
class: Acme\BaseBundle\Twig\UserExtension
arguments: ["@security.context"]
tags:
- { name: twig.extension }
这是我的课程
// acme/basebundle/twig/userextension.php
namespace Acme\BaseBundle\Twig;
use Symfony\Component\Security\Core\SecurityContext;
use Acme\UserBundle\Entity\User;
class UserExtension extends \Twig_Extension
{
protected $context;
public function __construct(SecurityContext $context){
$this->context = $context;
}
public function getFunctions()
{
return array(
'getAbcData' => new \Twig_SimpleFunction('getAbcData', $this->getAbcData()),
);
}
public function getAbcData()
{
if ( !is_object($user = $this->context->getToken()->getUser()) || !$user instanceof User){ return null; }
return array(
'data_array' => $user->getData(),
);
}
public function getName()
{
return 'user_extension';
}
}
最后,我有一个错误:
FatalErrorException: Error: Call to a member function getUser() on a non-object in \src\Acme\BaseBundle\Twig\UserExtension.php line 27
我想是 security.context 服务还没有初始化,然后我得到一个错误。
谁能告诉我,有什么方法可以手动加载服务,或者有更好的解决方案?
非常感谢。
I guess that security.context service is not initialized yet, then i get an error. Could anyone tell, please, is there are ways to load service manually, or any better solutions for an issue? Thanks a lot.
我使用Symfony 2.5。*
UPD:
我也在symfony文档中发现此通知
I've also found this notice in symfony docs
请记住,Twig Extensions不是延迟加载的。这意味着,如果有任何服务(或本例中的Twig扩展)依赖于请求服务,则您更有可能收到CircularReferenceException或ScopeWideningInjectionException。有关更多信息,请查看如何使用示波器。
实际上,我不知道如何纠正它。.
Keep in mind that Twig Extensions are not lazily loaded. This means that there's a higher chance that you'll get a CircularReferenceException or a ScopeWideningInjectionException if any services (or your Twig Extension in this case) are dependent on the request service. For more information take a look at How to Work with Scopes. Actually, I have no idea about how to do it correct..
推荐答案
您在构造Twig_SimpleFilter时正在调用 $ this-> getAbcData()
。但是您必须传递可调用作为参数。
You are calling $this->getAbcData()
when constructing Twig_SimpleFilter. But you have to pass a callable as argument.
public function getFunctions() {
return array (
'getAbcData' => new \Twig_SimpleFunction( 'getAbcData', array( $this, 'getAbcData' ))
);
}
狮子座也是对的。您应先检查 getToken()
是否返回对象,然后再尝试 getToken()-> getUser()
。
Leo is also right. You should check first if getToken()
is returning an object before trying getToken()->getUser()
.
您还可以将用户作为参数传递给树枝: {{getAbcData(app.user)}}
。这样,该功能将更通用,可用于任何用户,而不仅仅是当前登录的用户。
You can also pass the user to the function as a parameter in twig: {{ getAbcData(app.user) }}
. This way the function is more generic and could be used for any user, not just the currently logged in one.
这篇关于树枝扩展中的Symfony依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!