douzhuoxia0587 2018-09-25 15:16
浏览 49
已采纳

Symfony 2.7:无法加载类型

I am creating a simple contact form. My custom type starts this way:

<?php

namespace MyCompany\AppBundle\Form;

use Symfony\Component\Form\AbstractType;
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\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;

class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

... and my controller starts this way:

<?php

namespace MyCompany\AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class ContactController extends Controller
{
    public function contactAction(Request $request)
    {
        // Create the form according to the FormType created previously.
        // And give the proper parameters
        $form = $this->createForm('MyCompany\AppBundle\Form\ContactType',null,array(
            // To set the action use $this->generateUrl('route_identifier')
            'action' => $this->generateUrl('myapplication_contact'),
            'method' => 'POST'
        ));

... but I find that my application is unable to load the type. I get the following exception message:

Could not load type "MyCompany\AppBundle\Form\ContactType" 500 Internal Server Error - InvalidArgumentException

What can I do to start debugging this?

  • 写回答

2条回答 默认 最新

  • dongpo7467 2018-09-25 15:28
    关注

    Since you are using Symfony 2.7 the proper way to create a form in the controller using a form type class is:

    <?php
    
    namespace MyCompany\AppBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    
    use MyCompany\AppBundle\Form\ContactType; // add this
    
    class ContactController extends Controller
    {
        public function contactAction(Request $request)
        {
            // Create the form according to the FormType created previously.
            // And give the proper parameters
            $form = $this->createForm(new ContactType(), null, array(
                // To set the action use $this->generateUrl('route_identifier')
                'action' => $this->generateUrl('myapplication_contact'),
                'method' => 'POST'
            ));
    

    Refer to the symfony documentation

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog