src/Controller/AppointmentController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Appointment;
  4. use App\Entity\Booking;
  5. use App\Event\AppointmentEvent;
  6. use App\Form\AppointmentType;
  7. use App\Form\BookingType;
  8. use Boab\CmsBundle\Controller\BaseController;
  9. use Psr\Log\LoggerInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  14. class AppointmentController extends BaseController
  15. {
  16.     #[Route('/appointment/book'name'book_appointment')]
  17.     public function index(Request $requestEventDispatcherInterface $eventDispatcherLoggerInterface $logger): Response
  18.     {
  19.         $form $this->createForm(AppointmentType::class, new Appointment,[
  20.             "action" => $this->router->generate('book_appointment'),
  21.             "method" => "POST"
  22.         ]);
  23.         $form->handleRequest($request);
  24.         if($form->isSubmitted() && $form->isValid()){
  25.             $data $form->getData();
  26.             try{
  27.                 $this->entityManager->persist($data);
  28.                 $this->entityManager->flush();
  29.                 $this->eventDispatcher->dispatch(new AppointmentEvent($data), AppointmentEvent::SUMMITED);
  30.                 $this->addFlash('success''Appointment has been submitted successfully');
  31.             }catch(\Exception $e){
  32.                 dump($e);
  33.                 $logger->error($e->getMessage(), ['exception'=>$e]);
  34.                 $this->addFlash('danger''Something went wrong while processing submitting the appointment');
  35.             }
  36.             return $this->redirect($this->router->generate('book_appointment',['status'=>'success']));
  37.         }
  38.         return $this->render('booking/index.html.twig', [
  39.             'form' => $form->createView(),
  40.             'pageTitle' => 'Book Appointment'
  41.         ]);
  42.     }   
  43. }