Last active
March 9, 2018 21:15
-
-
Save donpadre/361163daf28855e87b37189b3851c826 to your computer and use it in GitHub Desktop.
ContactController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'layout.html.twig' %} | |
{% block title %}Nous Contacter{% endblock %} | |
{% block header %} | |
<header class="contact-bg text-center text-white"> | |
<div class="overlay"></div> | |
<div class="container"> | |
<div class="row"> | |
<div class="col-lg-8 col-md-10 mx-auto"> | |
<div class="site-heading"> | |
<h1 class="display-3 text-center pb-5">Nous contacter</h1> | |
</div> | |
</div> | |
</div> | |
</div> | |
</header> | |
{% endblock %} | |
{% block body %} | |
<section class="content-section"> | |
<div class="container"> | |
{% for message in app.flashes('success') %} | |
<div class="alert alert-success" role="alert"> | |
{{ message }} | |
</div> | |
{% endfor %} | |
<h2 class="display-4 text-center pb-5">Comment nous contacter ?</h2> | |
<div class="row"> | |
<div class="col-md-6 lead justify-content-center"> | |
<p>Une question ? Un problème ?</p> | |
<p>Ou simplement l'envie de nous contacter, n'hésitez pas !</p> | |
<p> Vous pouvez utiliser ce folumaire de contact ou nos reseaux socieaux afin de nous contacter.</p> | |
<p>Nous tâcherons de vous répondre rapidement. </p> | |
<p> | |
<ul class="list-inline mb-0 text-center"> | |
<li class="list-inline-item mr-3"> | |
<a href="https://www.facebook.com/ingameavecjesus/" target="_blank"> | |
<i class="fab fa-facebook fa-2x fa-fw"></i> | |
</a> | |
</li> | |
<li class="list-inline-item mr-3"> | |
<a href="https://twitter.com/in-game-avec-jesus" target="_blank"> | |
<i class="fab fa-twitter fa-2x fa-fw"></i> | |
</a> | |
</li> | |
<li class="list-inline-item"> | |
<a href="https://www.instagram.com/in-game-avec-jesus" target="_blank"> | |
<i class="fab fa-instagram fa-2x fa-fw"></i> | |
</a> | |
</li> | |
<li class="list-inline-item"> | |
<a href="https://www.youtube.com/channel/UCt_qqRpZ8KCfHEmw0GZus_A" target="_blank"> | |
<i class="fab fa-youtube fa-2x fa-fw"></i> | |
</a> | |
</li> | |
</ul> | |
</p> | |
</div> | |
<div class="col-md-6"> | |
<form action="{{ path('contact') }}" method="post"> | |
<div class="form-group"> | |
{{ form_widget(form.subject) }} {{ form_errors(form.subject) }} | |
</div> | |
<div class="form-group"> | |
{{ form_widget(form.name) }} {{ form_errors(form.name) }} | |
</div> | |
<div class="form-group"> | |
{{ form_widget(form.email) }} {{ form_errors(form.email) }} | |
</div> | |
<div class="form-group"> | |
{{ form_widget(form.message) }} {{ form_errors(form.message) }} | |
</div> | |
{# Render CSRF token etc .#} | |
<div style="display:none"> | |
{{ form_rest(form) }} | |
</div> | |
<input type="submit" value="Submit" class="btn btn-primary"> | |
</form> | |
</div> | |
</div> | |
</div> | |
</section> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Entity; | |
use Symfony\Component\Validator\Constraints as Assert; | |
class Contact | |
{ | |
/** | |
* | |
*/ | |
protected $name; | |
/** | |
* | |
* @Assert\Email( | |
* message = "L'adresse email'{{ value }}' n'est pas une adresse valide", | |
* checkMX = true | |
* ) | |
*/ | |
protected $email; | |
/** | |
* | |
* | |
* @Assert\Length( | |
* max = 50, | |
* maxMessage = "Your subject cannot be longer than {{ limit }} characters" | |
* ) | |
*/ | |
protected $subject; | |
/** | |
* | |
* @Assert\Length( | |
* min = 50, | |
* minMessage = "Your message must be at least {{ limit }} characters long" | |
* ) | |
*/ | |
protected $message; | |
public function getName() | |
{ | |
return $this->name; | |
} | |
public function setName($name) | |
{ | |
$this->name = $name; | |
} | |
public function getEmail() | |
{ | |
return $this->email; | |
} | |
public function setEmail($email) | |
{ | |
$this->email = $email; | |
} | |
public function getSubject() | |
{ | |
return $this->subject; | |
} | |
public function setSubject($subject) | |
{ | |
$this->subject = $subject; | |
} | |
public function getmessage() | |
{ | |
return $this->message; | |
} | |
public function setmessage($message) | |
{ | |
$this->message = $message; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller; | |
use App\Entity\Contact; | |
use App\Form\ContactType; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\Routing\Annotation\Route; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
class ContactController extends Controller | |
{ | |
/** | |
* @Route("/contact", name="contact") | |
* @Method({"GET", "POST"}) | |
* | |
* @param Request $request | |
* @param \Swift_Mailer $mailer | |
* @param ContainerInterface $container | |
* @return Response | |
*/ | |
public function contact(Request $request, \Swift_Mailer $mailer, ContainerInterface $container) | |
{ | |
$contact = new Contact(); | |
$form = $this->createForm(ContactType::class, ['contact' => $contact]); | |
if ($request->isMethod('POST')) { | |
// Refill the fields in case the form is not valid. | |
$form->handleRequest($request); | |
if ($form->isSubmitted() && $form->isValid()) { | |
$data = $form->getData(); | |
$message = (new \Swift_Message('Formulaire de contact')) | |
->setFrom($data["email"]) | |
->setTo($container->getParameter('mail_to')) | |
->setBody( | |
$this->renderView( | |
'contact/emails/contact.html.twig', | |
[ | |
'name' => $data["name"], | |
'email' => $data["email"], | |
'message' => $data["message"] | |
], | |
'text/html' | |
) | |
) | |
->addPart( | |
$this->renderView( | |
'contact/emails/contact.text.twig', | |
[ | |
'name' => $data["name"], | |
'email' => $data["email"], | |
'message' => $data["message"] | |
], | |
'text/plain' | |
) | |
); | |
return $mailer->send($message); | |
$this->addFlash('success', 'Merci pour votre message !'); | |
return $this->redirectToRoute('contact'); | |
} | |
} | |
return new Response( | |
$this->render('contact/contact.html.twig', [ | |
'form' => $form->createView() | |
]) | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Form; | |
use App\Entity\Contact; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\Validator\Constraints\Email; | |
use Symfony\Component\Validator\Constraints\NotBlank; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
use Symfony\Component\Form\Extension\Core\Type\TextType; | |
use Symfony\Component\Form\Extension\Core\Type\EmailType; | |
use Symfony\Component\Form\Extension\Core\Type\TextareaType; | |
class ContactType extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('name', TextType::class, array( | |
'attr' => array('placeholder' => 'Your name', "class" => "form-control"), | |
'constraints' => array( | |
new NotBlank(array("message" => "Please provide your name")) | |
) | |
)) | |
->add('subject', TextType::class, array( | |
'attr' => array('placeholder' => 'Subject', "class" => "form-control"), | |
'constraints' => array( | |
new NotBlank(array("message" => "Please give a Subject")) | |
) | |
)) | |
->add('email', EmailType::class, array( | |
'attr' => array('placeholder' => 'Your email address', "class" => "form-control"), | |
'constraints' => array( | |
new NotBlank(array("message" => "Please provide a valid email")), | |
new Email(array("message" => "Your email doesn't seems to be valid")) | |
) | |
)) | |
->add('message', TextareaType::class, array( | |
'attr' => array('placeholder' => 'Your message here', "class" => "form-control"), | |
'constraints' => array( | |
new NotBlank(array("message" => "Please provide a message here")) | |
) | |
)) | |
; | |
} | |
public function setDefaultOptions(OptionsResolver $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'error_bubbling' => true | |
)); | |
} | |
public function getName() | |
{ | |
return 'contact'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment