2013-08-06 4 views
1

에 대한 symfony2에 붙어 내가 무슨 짓을했는지 코드입니다 그리고 난여기 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 } 
입니다 내가 페이지를 열려고하면

지금의이 fallowing 말한다 : 어느 날 오류 메시지 상태로

답변

3

을 알려 주시기 바랍니다 문제가 무엇인지 알고있는 경우

"[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 "

을, 당신은 사용을 누락 귀하의 컨트롤러 파일 상단에 성명.

은 간단히 추가

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 

클래스 DefaultController 위에.

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

이 방법, 당신은 당신의 경로를 선언하는 기본 yml 방법 대신 @Route 주석을 사용하고 있습니다 :

당신은 다음과 라우팅을 대체 할 수 있습니다.

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

+0

당신이 지금 뭔가 다른 –

+0

FatalErrorException 올 감사합니다 그 문제를 해결하기 좋아요 : 오류 : 클래스 '알렉산더 \ IntelMarketingBundle \ 컨트롤러 \ ContactType'C에서 찾을 수 없습니다 : \ WAMP \ www가 \ 심포니 \ SRC \ 알렉산다르 \ IntelMarketingBundle \ Controller \ DefaultController.php line 38 –

+0

'DefaultContro ller.php' 위에 use 문이 없습니다. 그것을 해결하기 위해 다음 줄을 추가합니다 : 알렉산더 \ IntelMarketingBundle \ 양식 \ 타입 \ ContactType 사용',' – cheesemacfly