1

我在 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 }

现在,当我尝试打开页面时,它会说:

“[语义错误] Aleksandar\IntelMarketingBundle\Controller\DefaultController::contactAction() 方法中的注释“@Route”从未导入。您是否忘记为此注释添加“使用”语句?500 内部服务器错误 - AnnotationException "

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

4

1 回答 1

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

于 2013-08-06T16:09:44.447 回答