I made a search form in order to retrieve list of events. At the moment I have list of the events displayed, but I don't know how to display the "bigcity" and "category" name the user has selected previously in search form.
I worked well as I had "bigcity" and "category" names displayed before I focused on the list of events.
I wanted to be redirected to "events.html.twig" page, so I used the redirection to events route. The difference between my two controllers is simply that I replaced
return $this->render("front/events.html.twig", $data);
with
return $this->redirectToRoute('events', [$data]);
Currently I have this error :
Variable "category" does not exist
Now that my list of elements is displayed, variable "category" (and "bigcity" probably) isn't recognized any more. So I am not able to show the name of the "bigcity" and the name of the "category" in my title that user is supposed to select previously a search form (just before to be landed on events list).
How can I combine both, "bigcity" and "category" names with my events lists ?
Old EventsController.php file (when "bigcity" and "category" were displayed) :
<?php
namespace App\Controller\Front;
use App\Form\SearchType;
use App\Repository\EventsRepository;
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)
{
$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);
return $this->render("front/events.html.twig", $data);
}
return $this->renderForm('front/search.html.twig', [
'form' => $form
]);
}
}
Current EventsController.php file :
<?php
namespace App\Controller\Front;
use App\Form\SearchType;
use App\Repository\EventsRepository;
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)
{
$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);
return $this->redirectToRoute('events', [$data]);
}
return $this->renderForm('front/search.html.twig', [
'form' => $form
]);
}
#[Route('/events', name: 'events')]
public function events(
$data,
Request $request,
EventsRepository $eventsRepository,
CategoriesRepository $categoriesRepository
){
$data = $request->request->all();
$events = $eventsRepository->findAll();
$categories = $categoriesRepository->findAll();
return $this->render("front/events.html.twig", ['data' => $data, 'events' => $events, 'categories' => $categories]);
}
}
My events.twig file :
{% extends 'base.html.twig' %}
{% block title %}Liste des activités {{ category.title }} à {{ bigcity.name }}{% endblock %}
{% block main %}
<div >
<h1 >
<img src="{{ asset('img/logos/') }}{{ category.image }}" alt="{{ category.title }}" width="auto" height="40"> à <b >{{ bigcity.name }}</b>
</h1>
<div ></div>
<div >
<div >
<h2 >
<img src="{{ asset('img/titres/zpeak-sorties.svg') }}" alt="Les Zpeak Sorties !">
</h2>
<ul >
{% for event in events %}
{% if event and event.organizer.status == 'organizer' %}
<a >
<img src="{{ asset('img/flag_images/' ~ event.spokenlanguage.image) }}" alt="Drapeau {{ event.spokenlanguage.name }}" > {{ event.title }}
</a>
{% else %}
<p>Il n'y a pas de zpeak sortie organisée.</p>
{% endif %}
{% endfor %}
</ul>
</div>
<div >
<h2 >
<img src="{{ asset('img/titres/zpeak-idees.svg') }}" alt="Les Zpeak Idées !">
</h2>
<ul >
{% for event in events %}
{% if event and event.organizer.status == 'member' %}
<a >
<img src="{{ asset('img/flag_images/' ~ event.spokenlanguage.image) }}" alt="Drapeau {{ event.spokenlanguage.name }}" > {{ event.title }}
</a>
{% else %}
<p>Il n'y a pas de zpeak idée proposée.</p>
{% endif %}
{% endfor %}
</ul>
</div>
</div>
</div>
{% endblock %}
CodePudding user response:
The error is here
{% block title %}Liste des activités {{ category.title }} à {{ bigcity.name }}{% endblock %}
When you say {{ category.title }} or {{ bigcity.name }}, that means you call a variable category and basically (not alwyas) the variables are declared in the controller and passed to view in return render ...
In your case you call a variable category as an object, neither the variable category exists nor the object so how twig will know about them ?
the second thing, category may is exists in url as parameter but not as a full object, the id of category I guess (example &category=5&...)
So what you should do is to retrive this parameter in events function, if it exists so you must select the category having this id form the database or just select his name if you need just his name, and you pass this variable in return render and do not forget to do some test if exists to not get errors
#[Route('/events', name: 'events')]
public function events(Request $request, EventsRepository $eventsRepository, CategoriesRepository $categoriesRepository)
{
$data = $request->request->all();
$events = $eventsRepository->findAll();
$categories = $categoriesRepository->findAll();
$category = null;
if($request->query->has('category'))
{
$categoryId = $request->query->get('category');
// or
// $categoryId = $data['category'];
$category = $categoriesRepository->find($categoryId);
}
return $this->render("front/events.html.twig", [
'data' => $data,
'events' => $events,
'categories' => $categories,
'category' => $category // pass category to view
]);
}
{% if category is not null %}
{% block title %}Liste des activités {{ category.title }} à {{ bigcity.name }}{% endblock %}
{% endif %}