Symfony2:在服务中注入当前用户 [英] Symfony2: Inject current user in Service
问题描述
我正在尝试将当前登录的用户注入到服务中。我的目标是扩展一些twig功能,以便根据用户偏好输出它。在这个例子中,我想使用用户特定的时区输出任何日期函数。
I am trying to inject the currently logged in user into a service. My goal is to extend some twig functionality to output it based on user preferences. In this example I want to output any date function using the user specific Timezone.
似乎没有办法将当前用户注入服务,对我来说似乎真的很奇怪当注册安全上下文时,即使用户登录,它也没有令牌
There doesn't seem to be any way to inject the current user into a service, which seems really odd to me. When injecting the security context, it doesn't have a token even if the user is logged in
我正在使用FOS用户包。
I am using FOS user bundle.
services:
...
twigdate.listener.request:
class: App\AppBundle\Services\TwigDateRequestListener
arguments: [@twig, @security.context]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
<?php
namespace App\AppBundle\Services;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class TwigDateRequestListener
{
protected $twig;
function __construct(\Twig_Environment $twig, SecurityContext $context) {
$this->twig = $twig;
//$this->user = $context->get...;
var_dump($context); die;
}
public function onKernelRequest(GetResponseEvent $event) {
// $this->twig->getExtension('core')->setDateFormat($user->getProfile()->getFormat());
// $this->twig->getExtension('core')->setTimeZone($user->getProfile()->getTimezone());
}
}
output:
object(Symfony\Component\Security\Core\SecurityContext)[325]
private 'token' => null
private 'accessDecisionManager' =>
object(Symfony\Component\Security\Core\Authorization\AccessDecisionManager)[150]
private 'voters' =>
array
0 =>
object(Symfony\Component\Security\Core\Authorization\Voter\RoleHierarchyVoter)[151]
...
1 =>
object(Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter)[153]
...
2 =>
object(Symfony\Component\Security\Acl\Voter\AclVoter)[155]
...
private 'strategy' => string 'decideAffirmative' (length=17)
private 'allowIfAllAbstainDecisions' => boolean false
private 'allowIfEqualGrantedDeniedDecisions' => boolean true
private 'authenticationManager' =>
object(Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager)[324]
private 'providers' =>
array
0 =>
object(Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider)[323]
...
1 =>
object(Symfony\Component\Security\Core\Authentication\Provider\AnonymousAuthenticationProvider)[149]
...
private 'eraseCredentials' => boolean true
private 'alwaysAuthenticate' => boolean false
我错过了什么?
推荐答案
我将使用一个twig扩展名:
I would use a twig extension for that:
class UserDateExtension extends \Twig_Extension
{
private $context;
public function __construct(SecurityContext $context)
{
$this->context = $context;
}
public function getUser()
{
return $this->context->getToken()->getUser();
}
public function getFilters()
{
return array(
'user_date' => new \Twig_Filter_Method($this, "formatUserDate"),
);
}
public function formatUserDate($date, $format)
{
$user = $this->getUser();
// do stuff
}
现在在 services.xml
<service id="user_date_twig_extension" class="%user_date_twig_extension.class%">
<tag name="twig.extension" />
<argument type="service" id="security.context" />
</service>
然后在twig你可以做:
Then in twig you could do:
{{ date | user_date('d/m/Y') }}
这篇关于Symfony2:在服务中注入当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!