在 Symfony 中如何将数据形式传递到另一个路由?错误:必须为参数提供一个值

问题描述 投票:0回答:1

我构建了一个搜索表单,用户可以在其中选择城市和类别以发现可用的活动。

在我的控制器中,我创建了两条路线:搜索和事件。在搜索中,我定义了搜索表单,对于事件,我将用户重定向到 event.HTML.twig,他将在其中根据城市和类别找到事件列表。

因为我在此过程中使用两条路线,所以我很想将变量

$data
传递给第二条路线,该变量从搜索表单中检索数据。看起来它根本不起作用,因为我最终遇到了这个错误:

Controller "App\Controller\Front\EventsController::events()" requires that you provide a value for the "$data" argument. Either the argument is nullable and no null value has been provided, no default value has been provided, or there is a nonoptional argument after this one.

在 Symfony 中如何将

$data
值传递给第二条路由?

EventsController.php* 文件:

<?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' => $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]);
    }
}

Skuti 解决方案后编辑: 我尝试了第一条路线,但收到此错误:

Could not resolve argument $city of "App\Controller\Front\EventsController::searchform()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"?
#[Route('/search/form', name: 'events_search')]
    public function searchForm(): Response
    {
        $form = $this->createForm(SearchType::class, null, [
            'method' => 'GET',
            'action' => $this->generateUrl('events', ['category', 'city']),
        ]);

        return $this->renderForm('front/search.html.twig', [
            'form' => $form
        ]);
    }
php forms symfony variables controller
1个回答
0
投票

你好艾米丽,

这是我的建议:

<?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;

#[Route('/events')]
class EventsController extends AbstractController
{
    #[Route('/search/form', name: 'events_search_form')]
    public function searchForm(): Response
    {
        $form = $this->createForm(SearchType::class, null, [
            'method' => 'GET',
            'action' => $this->generateUrl('events_search', [
                'category' => null, // Or anything else if you want to directly filter
                'city' => null, // Or anything else if you want to directly filter
            ]),
        ]);

        return $this->renderForm('front/search.html.twig', [
            'form' => $form->createView(),
        ]);
    }

    #[Route('/search/{city}/{category}', name: 'events_search')]
    public function search(Request $request, EventsRepository $eventsRepository, string $city, string $category): Response
    {
        $form = $this->createForm(SearchType::class, null, [
            'method' => 'GET',
            'action' => $this->generateUrl('events_search', [
                'category' => $category,
                'city' => $city,
            ]),
        ]);
        $form->handlerRequest($request);

        $events = [];
        if ($form->isSubmitted() && $form->isValid()) {
            $events = $eventsRepository->findBy(array_merge($form->getData(), [
                'city' => $city,
                'category' => $category,
            ]);
        }

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

或者是一种。我当然没有测试它,事件查询必须由您自己创建,但您已经知道如何实现它。

© www.soinside.com 2019 - 2024. All rights reserved.