src/Controller/SubscriptionController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Subscriber;
  4. use App\Entity\Subscription;
  5. use App\Form\NewsletterType;
  6. use App\Form\SubscriptionType;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Boab\CmsBundle\Controller\BaseController;
  11. use Psr\Log\LoggerInterface;
  12. class SubscriptionController extends BaseController
  13. {
  14.     public function signup(Request $requestLoggerInterface $logger)
  15.     {
  16.         $form $this->createForm(SubscriptionType::class, new Subscription, [
  17.             "action" => $this->router->generate("app.subscription.signup"),
  18.             "method"=>"post"
  19.         ]);
  20.         
  21.         $form->handleRequest($request);
  22.         if($form->isSubmitted() && $form->isValid()){
  23.             $data $form->getData();
  24.             try{
  25.                 $this->save($data);
  26.                 return new JsonResponse([
  27.                     'status' => 'success',
  28.                     'message' => 'Thank you for subscribing'
  29.                 ]);
  30.             }catch(\Exception $e){
  31.                 $this->logger->error("error", ['exception'=>$e]);
  32.             }
  33.         }
  34.         
  35.         $view $this->viewManager->load("page/subscription.html.twig");
  36.         $view->form $form->createView();
  37.         return $view;
  38.     }
  39. }