Home > Blockchain >  How do I retrieve datas sent by search form when I go back to my events list?
How do I retrieve datas sent by search form when I go back to my events list?

Time:10-25

I'd like to know how I can go back to route events from event with the results obtained with the form at search. At the moment, when I try to go back from event to events, I lost all the datas that allowed me to find events according to their bigcity and their category.

Is there a way to retrieve these informations ?

event.html.twig

{% extends 'base.html.twig' %}

{% block main %}

<a href="{{ path('events') }}" >&lt; Retour</a>

EventController.php

<?php

namespace App\Controller\Front;

use App\Entity\Event;
use App\Form\SearchType;
use App\Repository\UserRepository;
use App\Repository\EventRepository;
use App\Repository\BigCityRepository;
use App\Repository\CategoryRepository;
use App\Repository\LocationRepository;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\ParticipationRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class EventController extends AbstractController
{
    #[Route('/search', name: 'search')]
    public function search(
        Request $request, 
        SessionInterface $sessionInterface,
        EventRepository $eventRepository,
    ){   
        $searchFormData  = $sessionInterface->get('searchFormData');

        $form = $this->createForm(SearchType::class, ['data' => $searchFormData]);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {

            $data = $form->getData();
            $sessionInterface->set('searchFormData', $data);
            $events = $eventRepository->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,
        EventRepository $eventRepository,
        CategoryRepository $categoryRepository,
        BigCityRepository $bigcityRepository,
        LocationRepository $locationRepository
    ){
        $category = $categoryRepository->findOneById($request->query->get('category'));
        $bigcity = $bigcityRepository->findOneById($request->query->get('bigcity'));
        $location = $locationRepository->findAll($request->query->get('location.bigcity'));

        $events = $eventRepository->comingEventsList(['category' => $category, 'address' => $location]);

        return $this->render("front/events.html.twig", [
            'events' => $events,
            'category' => $category,
            'bigcity' => $bigcity
        ]);
    }

    #[Route('/event/{id}', name: 'event', methods: ['GET'])]
    public function event(
        Event $event,
        UserRepository $userRepository,
        ParticipationRepository $participationRepository
    ){
        if ($this->getUser()) {

            $connected = $this->getUser();
            $useremail = $connected->getUserIdentifier();
            $user = $userRepository->findOneBy(['email' => $useremail]);

            if ($participationRepository->findby(['participant' => $user, 'event' => $event])) {
            
                $participation = $participationRepository->findby(['participant' => $user, 'event' => $event]);

                return $this->render('front/event.html.twig', [
                    'event' => $event,
                    'participation' => $participation
                ]);
        }}
        return $this->render('front/event.html.twig', [
            'event' => $event
        ]);
    }

CodePudding user response:

Each event is related to one category and one bigcity and the form search form has just this tow fields

So, when you are in the show one event route, you can know what is the category and what is the bigcity from the current event.

You can add those two attributes id in the path like this:

<a href="{{ path('events', {'category': event.category.id, 'bigcity': event.bigCity.id}) }}">Retour</a>

Or you can use "referer" also, but this will not work everytime because if the user comes to show one event from history of borwser or from an extern url or if he types the url directly in the browser url tab

 <a href="{{ app.request.headers.get('referer') }}">Rtour</a>

Or with javascript

  • Related