I'd like to lead users to a new route when he has submitted the search form. The new route will be named "events". In order to get all data that I need, I let him to stay on search. At the moment, it's the only way I have found to pass the two selections the user has made from search and events saved in database.
Thank you very much for help.
I am willing to share more about my code or show to you what I've tried.
<?php
namespace App\Controller\Front;
use App\Entity\Events;
use App\Entity\BigCity;
use App\Entity\Country;
use App\Entity\Language;
use App\Form\SearchType;
use App\Repository\EventsRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class EventsController extends AbstractController
{
#[Route('/search', name: 'search')]
public function search(
Request $request,
SessionInterface $sessionInterface,
EventsRepository $eventsRepository,
){
$data = $request->request->all();
$sessionSearchFormData = $sessionInterface->get('searchFormData');
$form = $this->createForm(SearchType::class, ['data' => $sessionSearchFormData]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$sessionInterface->set('searchFormData', $data);
$events = $eventsRepository->findAll();
return $this->render("front/events.html.twig", [
'bigcity'=> $form->get('bigcity')->getData()->getId(),
'category'=> $form->get('category')->getData()->getId(),
'events' => $events
]);
}
return $this->renderForm('front/search.html.twig', [
'form' => $form,
]);
}
#[Route('/events', name: 'events')]
public function events(
Request $request,
EventsRepository $eventsRepository,
CategoriesRepository $categoriesRepository,
BigCityRepository $bigCityRepository
){
$events = $eventsRepository->findAll();
$category = $categoriesRepository->find($request->query->get('category'));
$bigcity = $bigCityRepository->find($request->query->get('bigCity'));
return $this->render("front/events.html.twig", [
'events' => $events,
'category' => $category,
'bigcity' => $bigcity
]);
}
}
CodePudding user response:
The last time we have forget getId() here
'category'=> $form->get('category')->getData()
'bigcity'=> $form->get('bigcity')->getData()
and that's why you was redirect to localhost:8888/gozpeak/public/events? without any parameter
Now it should be like this
$queryStringParameters = http_build_query([
'category'=> $form->get('category')->getData()->getId(),
'bigcity'=> $form->get('bigcity')->getData()->getId()
]);
So based on your code this should works fine , tested on local. Just copy and paste the two functions in place of others if it works, you can edit them as you like later
#[Route('/search', name: 'search')]
public function search(Request $request, SessionInterface $sessionInterface)
{
$sessionSearchFormData = $sessionInterface->get('searchFormData');
$form = $this->createForm(SearchType::class, ['data' => $sessionSearchFormData]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$sessionInterface->set('searchFormData', $data);
$queryStringParameters = http_build_query([
'category'=> $form->get('category')->getData()->getId(),
'bigcity'=> $form->get('bigcity')->getData()->getId()
]);
$url = $this->generateUrl("events").'?'.$queryStringParameters;
return $this->redirect($url);
}
return $this->render('front/search.html.twig', [
'form' => $form->createView()
]);
}
#[Route('/events', name: 'events')]
public function events(Request $request, EventsRepository $eventsRepository, CategoriesRepository $categoriesRepository, BigCityRepository $bigCityRepository)
{
$events = $eventsRepository->findAll();
$categories = $categoriesRepository->findAll();
$category = null;
$bigcity = null;
if ($request->query->has('category')) {
$categoryId = $request->query->get('category');
$category = $categoriesRepository->find($categoryId);
}
if ($request->query->has('bigCity')) {
$bigCityId = $request->query->get('bigCity');
$bigcity = $bigCityRepository->find($bigCityId);
}
return $this->render("front/events.html.twig", [
'events' => $events,
'categories' => $categories,
'category' => $category,
'bigcity' => $bigcity
]);
}
CodePudding user response:
This is the solution :
EventsController.php
<?php
namespace App\Controller\Front;
use App\Entity\Events;
use App\Entity\BigCity;
use App\Entity\Country;
use App\Entity\Language;
use App\Form\SearchType;
use App\Repository\EventsRepository;
use App\Repository\BigCityRepository;
use App\Repository\CategoriesRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class EventsController extends AbstractController
{
#[Route('/search', name: 'search')]
public function search(
Request $request,
SessionInterface $sessionInterface,
EventsRepository $eventsRepository,
){
$data = $request->request->all();
$sessionSearchFormData = $sessionInterface->get('searchFormData');
$form = $this->createForm(SearchType::class, ['data' => $sessionSearchFormData]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$sessionInterface->set('searchFormData', $data);
$events = $eventsRepository->findAll();
return $this->redirectToRoute("events", [
'bigcity'=> $form->get('bigcity')->getData()->getId(),
'category'=> $form->get('category')->getData()->getId(),
'events' => $events
]);
}
return $this->renderForm('front/search.html.twig', [
'form' => $form,
]);
}
#[Route('/events', name: 'events')]
public function events(
Request $request,
EventsRepository $eventsRepository,
BigCityRepository $bigcityRepository,
CategoriesRepository $categoriesRepository
){
$events = $eventsRepository->findAll();
$category = $categoriesRepository->findOneById($request->query->get('category'));
$bigcity = $bigcityRepository->findOneById($request->query->get('bigcity'));
return $this->render("front/events.html.twig", [
'events' => $events,
'category' => $category,
'bigcity' => $bigcity
]);
}
}