2013-08-06 160 views
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('[email protected]') 
       ->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 } 

現在,當我嘗試打開網頁的說休耕:

"[Semantical Error] The annotation "@Route" in method Aleksandar\IntelMarketingBundle\Controller\DefaultController::contactAction() was never imported. Did you maybe forget to add a "use" statement for this annotation? 500 Internal Server Error - AnnotationException "

如果有任何人知道可能是什麼問題,請讓我知道

回答

3

隨着錯誤消息狀態,你缺少一個使用聲明在你的控制器文件之上。

只需添加:

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

+0

好它解決這個問題謝謝現在別的東西拿出 –

+0

FatalErrorException:錯誤:類 '亞歷山大\ IntelMarketingBundle \控制器\ ContactType' 沒有發現在C:\ WAMP \ WWW \ symfony中的\ src \亞歷山大\ IntelMarketingBundle \ Controller \ DefaultController.php line 38 –

+0

您錯過了'DefaultContro ller.php'上方的使用說明。添加以下行來解決它:'使用Aleksandar \ IntelMarketingBundle \ Form \ Type \ ContactType;' – cheesemacfly