如何从event路由返回events路由时保留搜索表单数据?
解决从
event路由返回events时保留筛选结果的问题 问题核心是:从event页面返回events时,返回链接没有携带之前的bigcity和category筛选参数,导致筛选状态丢失。以下是两种可行的解决方案:
方案一:利用Session存储的筛选数据(推荐)
你已经在search方法中将筛选数据存入了Session的searchFormData,直接读取该数据并传递到返回链接即可:
1. 修改EventController的event方法
注入SessionInterface,读取存储的筛选参数并传给模板:
#[Route('/event/{id}', name: 'event', methods: ['GET'])] public function event( Event $event, UserRepository $userRepository, ParticipationRepository $participationRepository, SessionInterface $sessionInterface // 新增注入 ){ // 从Session获取之前的筛选数据,默认空数组避免报错 $searchFormData = $sessionInterface->get('searchFormData', []); $bigcityId = $searchFormData['bigcity']?->getId() ?? null; $categoryId = $searchFormData['category']?->getId() ?? null; if ($this->getUser()) { $connected = $this->getUser(); $useremail = $connected->getUserIdentifier(); $user = $userRepository->findOneBy(['email' => $useremail]); // 改用findOneBy获取单个参与记录,避免返回数组 if ($participation = $participationRepository->findOneBy(['participant' => $user, 'event' => $event])) { return $this->render('front/event.html.twig', [ 'event' => $event, 'participation' => $participation, 'bigcityId' => $bigcityId, 'categoryId' => $categoryId ]); } } return $this->render('front/event.html.twig', [ 'event' => $event, 'bigcityId' => $bigcityId, 'categoryId' => $categoryId ]); }
2. 修改event.html.twig的返回链接
在path函数中添加筛选参数:
<a href="{{ path('events', {'bigcity': bigcityId, 'category': categoryId}) }}">< Retour</a>
3. 优化events方法的参数处理(可选)
避免参数为空时出现错误,调整参数获取逻辑:
#[Route('/events', name: 'events')] public function events( Request $request, EventRepository $eventRepository, CategoryRepository $categoryRepository, BigCityRepository $bigcityRepository, LocationRepository $locationRepository ){ // 仅当参数存在时才查询实体 $category = $request->query->get('category') ? $categoryRepository->findOneById($request->query->get('category')) : null; $bigcity = $request->query->get('bigcity') ? $bigcityRepository->findOneById($request->query->get('bigcity')) : null; // 修正原location查询逻辑,关联bigcity筛选 $location = $bigcity ? $locationRepository->findBy(['bigcity' => $bigcity]) : $locationRepository->findAll(); $events = $eventRepository->comingEventsList(['category' => $category, 'address' => $location]); return $this->render("front/events.html.twig", [ 'events' => $events, 'category' => $category, 'bigcity' => $bigcity ]); }
方案二:通过URL传递筛选参数
如果不想依赖Session,可以在进入event页面时直接把筛选参数带在URL里:
1. 修改events.html.twig中的活动链接
添加bigcity和category参数:
<!-- 假设原活动链接是这样,追加筛选参数 --> <a href="{{ path('event', {'id': event.id, 'bigcity': bigcity.id, 'category': category.id}) }}">查看活动详情</a>
2. 修改EventController的event方法
从Request中获取URL参数并传递给模板:
#[Route('/event/{id}', name: 'event', methods: ['GET'])] public function event( Event $event, UserRepository $userRepository, ParticipationRepository $participationRepository, Request $request // 新增注入 ){ // 从URL查询参数中获取筛选值 $bigcityId = $request->query->get('bigcity'); $categoryId = $request->query->get('category'); if ($this->getUser()) { $connected = $this->getUser(); $useremail = $connected->getUserIdentifier(); $user = $userRepository->findOneBy(['email' => $useremail]); if ($participation = $participationRepository->findOneBy(['participant' => $user, 'event' => $event])) { return $this->render('front/event.html.twig', [ 'event' => $event, 'participation' => $participation, 'bigcityId' => $bigcityId, 'categoryId' => $categoryId ]); } } return $this->render('front/event.html.twig', [ 'event' => $event, 'bigcityId' => $bigcityId, 'categoryId' => $categoryId ]); }
3. 修改event.html.twig的返回链接
和方案一一致,带上筛选参数:
<a href="{{ path('events', {'bigcity': bigcityId, 'category': categoryId}) }}">< Retour</a>
额外优化:清理无用代码
在search方法的重定向中,events参数是多余的(无法通过路由传递对象数组),直接删除:
return $this->redirectToRoute("events", [ 'bigcity'=> $form->get('bigcity')->getData()->getId(), 'category'=> $form->get('category')->getData()->getId() ]);
内容的提问来源于stack exchange,提问作者Emilie Tossan
相关产品推荐
相关产品推荐

