坚持使用symfony2获取联系表格

时间:2013-08-06 15:51:30

标签: symfony contact-form

我在symfony2中遇到联系表单有问题这里是我做过的代码和我得到的错误

<?php
// src/Aleksandar/IntelMarketingBundle/Resources/views/ContactType.php
namespace Aleksandar\IntelMarketingBundle\Resources\views;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Collection;


class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'text', array(
                'attr' => array(
                    'placeholder' => 'What\'s your name?',
                    'pattern'     => '.{2,}' //minlength
                )
            ))
            ->add('email', 'email', array(
                'attr' => array(
                    'placeholder' => 'So I can get back to you.'
                )
            ))
            ->add('subject', 'text', array(
                'attr' => array(
                    'placeholder' => 'The subject of your message.',
                    'pattern'     => '.{3,}' //minlength
                )
            ))
            ->add('message', 'textarea', array(
                'attr' => array(
                    'cols' => 20,
                    'rows' => 2,
                    'placeholder' => 'And your message to me...'
                )
            ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $collectionConstraint = new Collection(array(
            'name' => array(
                new NotBlank(array('message' => 'Name should not be blank.')),
                new Length(array('min' => 2))
            ),
            'email' => array(
                new NotBlank(array('message' => 'Email should not be blank.')),
                new Email(array('message' => 'Invalid email address.'))
            ),
            'subject' => array(
                new NotBlank(array('message' => 'Subject should not be blank.')),
                new Length(array('min' => 3))
            ),
            'message' => array(
                new NotBlank(array('message' => 'Message should not be blank.')),
                new Length(array('min' => 5))
            )
        ));

        $resolver->setDefaults(array(
            'constraints' => $collectionConstraint
        ));
    }

    public function getName()
    {
        return 'contact';
    }
}
?>

这是将在视图中呈现的联系表单的代码 这里没有来自我的控制器的代码

<?php

namespace Aleksandar\IntelMarketingBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{

/**
 * @Route("/contact", _name="contact")
 * @Template()
 */      

         public function contactAction()
    {

    $form = $this->createForm(new ContactType());

    if ($request->isMethod('POST')) {
        $form->bind($request);

        if ($form->isValid()) {
            $message = \Swift_Message::newInstance()
                ->setSubject($form->get('subject')->getData())
                ->setFrom($form->get('email')->getData())
                ->setTo('info@intelmarketing.es')
                ->setBody(
                    $this->renderView(
                        'AleksandarIntelMarketingBundle::contact.html.php',
                        array(
                            'ip' => $request->getClientIp(),
                            'name' => $form->get('name')->getData(),
                            'message' => $form->get('message')->getData()
                        )
                    )
                );

            $this->get('mailer')->send($message);

            $request->getSession()->getFlashBag()->add('success', 'Your email has been sent! Thanks!');

            return $this->redirect($this->generateUrl('contact'));
        }
    }

    return array(
        'form' => $form->createView()
    );

    }


}

这是生根

aleksandar_intel_marketing_contactpage:
    pattern:  /contact
    defaults: { _controller: AleksandarIntelMarketingBundle:Default:contact }

现在,当我试图打开页面时,它说出了休息:

  

“[语义错误]方法中的注释”@Route“   亚历山大\ IntelMarketingBundle \控制器\ DefaultController :: contactAction()   从未进口过。您是否忘记添加“使用”声明   这个注释? 500内部服务器错误 - AnnotationException“

如果有人知道可能是什么问题,请告诉我

1 个答案:

答案 0 :(得分:3)

如错误消息所示,您在控制器文件上缺少一个use语句。

只需添加:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

在您的课程DefaultController之上。

然后,您可以使用以下命令替换路由:

aleksandar_intel_marketing:
    resource: "@AleksandarIntelMarketingBundle/Controller/DefaultController.php"
    type:     annotation

这样,您使用@Route注释而非默认yml方式来声明路线。

http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html