Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为FOSUserBundle注册页面创建角色选项列表_Php_Symfony_Fosuserbundle - Fatal编程技术网

Php 为FOSUserBundle注册页面创建角色选项列表

Php 为FOSUserBundle注册页面创建角色选项列表,php,symfony,fosuserbundle,Php,Symfony,Fosuserbundle,我正试图实现所涵盖主题的答案,但我遇到了问题 Q 加载注册表时出现以下错误 Notice: Undefined index: roles 关于 in src/Ampisoft/Bundle/etrackBundle/Form/Type/RegistrationFormType.php at line 21 RegistrationFormType::RegisterAction 我在这段代码的第二行测试了$roles的输出,我得到了预期的完整数组 //....... $roles = $th

我正试图实现所涵盖主题的答案,但我遇到了问题

Q 加载注册表时出现以下错误

Notice: Undefined index: roles
关于

in src/Ampisoft/Bundle/etrackBundle/Form/Type/RegistrationFormType.php at line 21
RegistrationFormType::RegisterAction

我在这段代码的第二行测试了$roles的输出,我得到了预期的完整数组

//.......
$roles = $this->container->getParameter('security.role_hierarchy.roles');
    var_dump($roles);
    return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
        'form' => $form->createView(
            array('roles' => $roles,
            )),
    ));
// .......
问题似乎在于我将$roles数组传递或检索到Type类的方式

注册表单类型


我尝试了各种方法将security.hierarchy.roles数组的内容传递给Type类,但没有成功。

控制器如何调用RegistrationFormType?第一个片段是来自RegistrationController的默认FOSUserBundle代码。我不知道它怎么称呼它,因为它不是很明显。我所做的就是给它传递一个数组。
class RegistrationFormType extends AbstractType
{
    protected $roles;

    public function __construct($options = array())
    {
        $this->roles = $options['roles']; // <--------------- THIS IS line 21
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // custom form fields
        $builder->add('firstname');
        $builder->add('lastname');
        $builder->add('roles', 'choice', array(
            'required' => true,
            'multiple' => true,
            'choices' => $this->refactorRoles($this->roles)
        ));
    }

    public function getDefaultOptions()
    {
        return array(
            'roles' => null
        );
    }

    private function refactorRoles($originRoles)
    {
        $roles = array();
        $rolesAdded = array();

        // Add herited roles
        foreach ($originRoles as $roleParent => $rolesHerit) {
            $tmpRoles = array_values($rolesHerit);
            $rolesAdded = array_merge($rolesAdded, $tmpRoles);
            $roles[$roleParent] = array_combine($tmpRoles, $tmpRoles);
        }
        // Add missing superparent roles
        $rolesParent = array_keys($originRoles);
        foreach ($rolesParent as $roleParent) {
            if (!in_array($roleParent, $rolesAdded)) {
                $roles['-----'][$roleParent] = $roleParent;
            }
        }

        return $roles;
    }

    public function getParent()
    {
        return 'fos_user_registration';
    }

    public function getName()
    {
        return 'etrack_user_registration';
    }

}