Answer the question
In order to leave comments, you need to log in
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,
]);
}
}
<div class="card">
{{ form_start(form) }}
{{ form_widget(form) }}
<button type="submit" class="btn btn-success banner-form-btn">Показать</button>
{{ form_end(form) }}
</div>
/* 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()]);
}
}
No route found for "GET /search" (from " localhost:8080 ")
http://localhost:8080/search?region=9d326bb2-96cd-4cd6-a3ea-508b4ade3c83&city=6e520d96-9559-4d67-9a90-a56e1defa1a1&_token=l5K7J4XGoU89zfo4gT6dGNh7VW-KXFAhvIvbVSfeyPY
http://localhost:8080/search/9d326bb2-96cd-4cd6-a3ea-508b4ade3c83/6e520d96-9559-4d67-9a90-a56e1defa1a1
Answer the question
In order to leave comments, you need to log in
Routes /search/{region}/{city}
and /search
different 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 questionAsk a Question
731 491 924 answers to any question