Symfony2: Getting the list of user roles in FormBuilder

前端 未结 10 1424
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-08 15:55

I\'m making a form for user creation, and I want to give one or several roles to a user when I create him.

How do I get the list of roles defined in security.y

10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 16:51

    //FormType
    use Symfony\Component\Yaml\Parser;
    
    function getRolesNames(){
            $pathToSecurity = /var/mydirectory/app/config/security.yml
            $yaml = new Parser();
            $rolesArray = $yaml->parse(file_get_contents($pathToSecurity ));
    
            return $rolesArray['security']['role_hierarchy']['ROLE_USER'];
    }
    

    This is so far the best way i found to get or set what i want from config files.

    Bon courage

提交回复
热议问题