deblan.io-murph/src/Controller/ContactController.php
2021-03-29 14:33:46 +02:00

48 lines
1.4 KiB
PHP

<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Core\Controller\Site\PageController;
use App\Form\ContactType;
use App\Core\Notification\MailNotifier;
use Symfony\Component\HttpFoundation\Request;
class ContactController extends PageController
{
public function contact(MailNotifier $notifier, Request $request): Response
{
$form = $this->createForm(ContactType::class);
$showForm = true;
if ($request->isMethod('POST')) {
$form->handleRequest($request);
if ($form->isValid()) {
$showForm = false;
$data = $form->getData();
$notifier
->addRecipient('simon@deblan.fr')
->setSubject('[Deblan] Nouveau message')
->setReplyTo($data['email'])
->notify('mail/contact.html.twig', [
'message' => $data,
], 'text/plain');
$this->addFlash(
'success',
'Votre message a bien été envoyé.'
);
}
}
return $this->defaultRender($this->siteRequest->getPage()->getTemplate(), [
'form' => $form->createView(),
'showForm' => $showForm,
]);
}
}