D
D
Dmitry2021-03-18 19:48:47
symfony
Dmitry, 2021-03-18 19:48:47

How to solve the problem with the route?

Good evening.
There is a form on the main page of the site that submits data to the search page.
The GET method is used.

class Form extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->setAction('/search')
            ->setMethod('get')
            ->setAttribute('name', '')
            ->add('region', Type\ChoiceType::class, [
                'label' => false,
                'choices' => array_flip($this->regions->assoc()),
                'attr' => ['class' => 'form-select'],
                'placeholder' => 'Край/Область'
            ])
            ->add('city', Type\ChoiceType::class, [
                'label' => false,
                'choices' => [],
                'attr' => ['class' => 'form-select', 'disabled' => true],
                'placeholder' => 'Город'
            ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Filter::class,
        ]);
    }
}

In template
<div class="card">
    {{ form_start(form) }}
    {{ form_widget(form) }}
    <button type="submit" class="btn btn-success banner-form-btn">Показать</button>
    {{ form_end(form) }}
</div>

Form is created in SiteController, form action leads to SearchController
/* SiteController */
    public function index(Request $request): Response
    {
        $filter = new Frontend\Filter();
        //$form = $this->createForm(Frontend\Form::class, $filter);
        $form = $this->get('form.factory')->createNamed('',  Frontend\Form::class, $filter);
        $form->handleRequest($request);
        return $this->render('app/site/index.html.twig', ['form' => $form->createView()]);
    }

/* SearchController */

/**
 * Class SearchController
 * @package App\Controller
 *
 * @Route("/search", name="search")
 */
class SearchController extends AbstractController
{
    /**
     * @param string $region
     * @param string $city
     * @param Request $request
     * @return Response
     *
     * @Route("/{region}/{city}", name="", methods={"GET"})
     */
    public function index(string $region, string $city, Request $request): Response
    {
       /......./
        return $this->render('app/site/search.html.twig', ['companies' => $companies, 'form' => $form->createView()]);
    }
}


After submitting the form, the route is "lost", resulting in an error:
No route found for "GET /search" (from " localhost:8080 ")

And in the address bar this address
http://localhost:8080/search?region=9d326bb2-96cd-4cd6-a3ea-508b4ade3c83&city=6e520d96-9559-4d67-9a90-a56e1defa1a1&_token=l5K7J4XGoU89zfo4gT6dGNh7VW-KXFAhvIvbVSfeyPY

If you create a regular link on the main page and perform the transition, then everything works fine, the route is found, the data is displayed, in the address bar the address you need
http://localhost:8080/search/9d326bb2-96cd-4cd6-a3ea-508b4ade3c83/6e520d96-9559-4d67-9a90-a56e1defa1a1


How to solve this problem?

UPD.
In the future, instead of id, a slug from the names of regions and cities will be substituted in the query string. Is it possible to replace id with slug with this approach?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2021-03-18
@slo_nik

Routes /search/{region}/{city}and /searchdifferent routes
I would go with query parameters and resolve them to controller parameters like this:
https://stackoverflow.com/questions/33982299/symfo...
Action example

/**
 * @Route("/search")
 * @ParamConverter("region", converter="querystring")
 * @ParamConverter("city", converter="querystring")
 */
public function index(string $region,  string $city) 
{
  return new Response(print_r($name, true));
}

class QueryStringConverter implements ParamConverterInterface
{
    public function supports(ParamConverter $configuration) 
    {
        return 'querystring' == $configuration->getConverter();
    }

    public function apply(Request $request, ParamConverter $configuration) 
    {
        $param = $configuration->getName();
        if (!$request->query->has($param)) {
            return false;
        }
        $value = $request->query->get($param);
        $request->attributes->set($param, $value);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question