src/Controller/Frontend/SearchController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Entity\News;
  4. use App\Entity\Section;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  7. use Symfony\Component\Form\Extension\Core\Type\TextType;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use App\Form\Frontal\SearchType;
  12. class SearchController extends AbstractController
  13. {
  14.     /**
  15.      * @Route(
  16.      *   {
  17.      *       "es" : "{_locale}/resultados-busqueda",
  18.      *       "ca" : "{_locale}/resultats-busqueda",
  19.      *       "en" : "{_locale}/search-results",
  20.      *       "cn" : "{_locale}/search-results",
  21.      *    }, name="search",
  22.      *       requirements={
  23.      *          "_locale": "es|ca|en|cn",
  24.      *          "_format" : "html"
  25.      *      }
  26.      * )
  27.      */
  28.     public function search(Request $request): Response
  29.     {
  30.         $words $request->request->get('search')['query'];
  31.         return $this->render('Frontend/search/search_results.html.twig', [
  32.             'currentSection' => $this->getDoctrine()->getRepository(Section::class)->getOneByLang('search'$request->getLocale()),
  33.             'news' => $this->getDoctrine()->getRepository(News::class)->search($words$request->getLocale()),
  34.             'words' => $words
  35.         ]);
  36.     }
  37.     public function drawForm(Request $request)
  38.     {
  39.         $oForm $this->createForm(SearchType::class, null, [
  40.             'action' => $this->generateUrl('search'),
  41.         ]);
  42.         return $this->render('Frontend/search/search_form.html.twig', [
  43.             'form' => $oForm->createView()
  44.         ]);
  45.     }
  46. }