无法在 symfony3 中加载类型

Could not load type in symfony3

我有 symfony 3.3 并且在创建表单时出现这个问题。

在控制器中实例化表单时出现错误。

以下是涉及的类:

表格.

namespace AppBundle\Form;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use AppBundle\Entity\Departamento;
use Symfony\Component\Form\AbstractType;


class DepartamentoType extends AbstractType {

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nombreDepartamento',TextType::class)
            ->add('planta', IntegerType::class)
            ->add('edificio',TextType::class)
            ->add('ciudadDepartamento',TextType::class)    
        ;
    }
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => Departamento::class
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'appbundle_departamento';
    }


}

控制器。

namespace AppBundle\Controller;

use AppBundle\Entity\Departamento;
use AppBundle\Form\DepartamentoType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DepartamentoController extends Controller{

    /**
     * @Route("/departamento_new", name="departamento_new")
     */
    public function newAction(Request $request)
    {
        $departamento = new Departamento();
        $formulario = $this->createForm(
            DepartamentoType::class, 
            $departamento,
            array('action' => $this->generateUrl('departamento_create'),
                  'method' => 'POST')
        );
        return $this->render('departamento/new_edit.html.twig', [
            'formulario' => $formulario->createView(),
        ]);
    }

这就是我尝试创建表单的方式。

这是错误:

Could not load type "AppBundle\Form\DepartamentoType" Symfony\Component\Form\Exception\InvalidArgumentException

autoconfigure 选项已添加到 Symfony 3.3

如果您不使用 autoconfigure,请确保 tag 使用 form.type 您的服务。

例如:

app.departamento:
    class: AppBundle\Form\DepartamentoType
    tags:
        - { name: form.type }

仅供参考:

使用表单类型服务定义,修复了以下错误。

Could not load type "AppBundle\Form\DepartamentoType" Symfony\Component\Form\Exception\InvalidArgumentException"

(我认为)@Lucas Leonel Azzollini 的特例,他遇到了以下错误。

Attempted to load class "DepartamentoType" from namespace "AppBundle\Form". Did you forget a "use" statement for another namespace?

在他的项目中,DepartamentoType.php 文件路径在 src\Form 中。然后他将文件移动到 AppBundle 目录中,如下所示:src\AppBundle\Form,并修复了错误。