从验证器获取用户| Symfony2的

时间:2016-12-21 13:07:36

标签: php symfony

在我的SF2项目中,我使用了一个CongeValidator类,其中包含一个if,它可以调用服务CongesService中的函数:

if (!$this->congeService->RTTCheck($conge)) {}

调用的函数看起来像:

public function RTTCheck($conge, $user){
    // $user         = $this->get('security.context')->getToken()->getUser();
    $nbJoursConge = $this->getNombreJoursPoses($conge->getCngDateDebut(), $conge->getCngDateFin(), $conge->getCngDateDebutDemi(), $conge->getCngDateFinDemi(), $user);
    $restants     = $this->RTT_restants($user);
    if(fmod($restants, 0.5)==0)
        if($nbJoursConge-floor($restants) >= 0) return true;
    return false;
}

正如您所看到的,我的问题是我需要检索用户(当前的用户,也可以作为参数)来使用该功能。 但是这个函数永远不会在Controller中调用,只能在Validator中调用:/所以我不知道如何正确地使用>谋杀用户。

你知道我怎么能管理它吗​​?提前谢谢。

1 个答案:

答案 0 :(得分:0)

您必须在服务中注入securityContext或TokenStorage(取决于您的symfony版本)。

然后你可以像这样打电话给你的用户

$context->getToken()->getUser()