Php 无法加载类型“;应用程序“用户注册”;尝试向注册表中添加字段时

Php 无法加载类型“;应用程序“用户注册”;尝试向注册表中添加字段时,php,symfony,fosuserbundle,Php,Symfony,Fosuserbundle,我正在用Doctrine和FOSBundle 2.0在Symfony3中开发一个应用程序 我一直试图在我的注册栏中添加两个字段,firstName和lastName ,不幸的是,经过最初的几个步骤(处理之前),我意识到我遇到了以下错误: 无法加载类型“应用程序用户注册” 我使用的代码是从webiste复制的,唯一的区别是我的类看起来像这样 <?php // src/AppBundle/Entity/User.php namespace AppBundle\Entity; use FOS

我正在用Doctrine和FOSBundle 2.0在Symfony3中开发一个应用程序

我一直试图在我的注册栏中添加两个字段,firstNamelastName

,不幸的是,经过最初的几个步骤(处理之前),我意识到我遇到了以下错误:

无法加载类型“应用程序用户注册”

我使用的代码是从webiste复制的,唯一的区别是我的类看起来像这样

<?php
// src/AppBundle/Entity/User.php

namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /** @ORM\Column(type="integer") */
    protected $carma;

    /**
     * @ORM\Column(type="string", length=255)
     *
     * @Assert\NotBlank(message="Please enter your first name.", groups={"Registration", "Profile"})
     * @Assert\Length(
     *     min=3,
     *     max=255,
     *     minMessage="The name is too short.",
     *     maxMessage="The name is too long.",
     *     groups={"Registration", "Profile"}
     * )
     */
    protected $firstName;

    /**
     * @ORM\Column(type="string", length=255)
     *
     * @Assert\NotBlank(message="Please enter your first name.", groups={"Registration", "Profile"})
     * @Assert\Length(
     *     min=3,
     *     max=255,
     *     minMessage="The name is too short.",
     *     maxMessage="The name is too long.",
     *     groups={"Registration", "Profile"}
     * )
     */
    protected $lastName;


//registrationtype.php

我怀疑您也在尝试按照教程扩展注册表。您很可能在app/config/services.yml文件中有以下条目:

app.form.registration:
    class: AppBundle\Form\RegistrationType
    tags:
        - { name: form.type, alias: app_user_registration }
然后在src/AppBundle/Form/RegistrationType.php中,您应该具有以下功能:

public function getBlockPrefix()
{
    return 'app_user_registration';
}

这很可能是getBlockPrefix()方法中的错误来源。查看你的services.yml并与我发布的内容进行比较。我是从我自己的应用程序中获取的,该应用程序运行良好。

你应该改用这个tuorial(2.0.master):

您正在使用教程“1.3.x版本”

对于Symfony3,您必须进行以下更改:

class RegistrationType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('name'); } public function getParent() { return 'FOS\UserBundle\Form\Type\RegistrationFormType'; } public function getBlockPrefix() { return 'app_user_registration'; } } 类注册类型扩展了AbstractType { 公共函数buildForm(FormBuilderInterface$builder、array$options) { $builder->add('name'); } 公共函数getParent() { 返回'FOS\UserBundle\Form\Type\RegistrationFormType'; } 公共函数getBlockPrefix() { 返回“应用程序用户注册”; } } 服务: app.form.registration: 类别:AppBundle\Form\RegistrationType 标签: -{name:form.type,别名:app\u user\u registration} fos_用户: # ... 注册: 表格: 类型:AppBundle\Form\RegistrationType
此错误不在您的用户实体中,它来自于尝试使用您在app/config/services.yml中设置的别名来引用您的用户注册表单。您是否也执行表单类型操作,因为这就是错误的来源。在寻找其他例子时要小心。您发布的第二个链接是针对旧版本的FOSUserBundle的。事实上,您会发现大多数示例都过时了。我已经意识到了这个问题,它实际上存在于我的
getParent()
函数中,因为它在应该返回
FOS\UserBundle\Form\Type\RegistrationFormType
时返回了
app\u user\u registration
app.form.registration:
    class: AppBundle\Form\RegistrationType
    tags:
        - { name: form.type, alias: app_user_registration }
public function getBlockPrefix()
{
    return 'app_user_registration';
}
class RegistrationType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('name'); } public function getParent() { return 'FOS\UserBundle\Form\Type\RegistrationFormType'; } public function getBlockPrefix() { return 'app_user_registration'; } } services: app.form.registration: class: AppBundle\Form\RegistrationType tags: - { name: form.type, alias: app_user_registration } fos_user: # ... registration: form: type: AppBundle\Form\RegistrationType