src/Controller/ContactController.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\ContactFormType;
  4. use App\Service\TelegramService;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. class ContactController extends AbstractController
  11. {
  12.   /*
  13.   // ==============================
  14.   // ИНИЦИАЛИЗАЦИЯ TelegramService
  15.   // (СЕЙЧАС ОТКЛЮЧЕНА)
  16.   // ==============================
  17.   private $telegramService;
  18.   public function __construct()
  19.   {
  20.     $telegramToken = '6182021373:AAFhsm39Y-enN9vjT1pXIEeToCw1DbOnfqM';
  21.     $telegramChatId = '-721778259';
  22.     $this->telegramService = new TelegramService($telegramToken, $telegramChatId);
  23.   }
  24.   */
  25.   /**
  26.    * @Route("/contact", name="contact", methods={"GET", "POST"})
  27.    */
  28.   public function contact(Request $request): Response
  29.   {
  30.     /*
  31.     // ======================================
  32.     // ОБРАБОТКА POST И ОТПРАВКА В TELEGRAM
  33.     // (СЕЙЧАС ОТКЛЮЧЕНО)
  34.     // ======================================
  35.     if ($request->isMethod('POST')) {
  36.       // Обработка отправленных данных формы
  37.       $text = $request->request->get('text');
  38.       $phone = $request->request->get('phone');
  39.       // и так далее
  40.       // Отправка сообщения в Telegram
  41.       $message = 'Новое сообщение из формы обратной связи:';
  42.       $message .= "\nТекст: " . $text;
  43.       $message .= "\nНомер телефона: " . $phone;
  44.       // и так далее
  45.       $this->telegramService->sendMessage($message);
  46.       // Возвращаем JSON-ответ в случае AJAX-запроса
  47.       if ($request->isXmlHttpRequest()) {
  48.         $responseData = [
  49.           'success' => true,
  50.           'message' => 'Сообщение удачно отправлено',
  51.         ];
  52.         return new JsonResponse($responseData);
  53.       }
  54.       // Возвращаем полную HTML-страницу в случае не-AJAX-запроса
  55.       return $this->redirectToRoute('contact'); // или другой редирект
  56.     }
  57.     */
  58.     // Сейчас просто отображаем страницу без обработки формы
  59.     return $this->render('contact/contact.html.twig');
  60.   }
  61. }