So I have an url like this: localhost:8000/answer/create?id=254.
The full way to get is: localhost:8000/questions -> localhost:8000/questions/{question_id} -> localhost:8000/answer/create?id=254.
How to make an app read this id=254 as a question_id?
Part of Controller:
/**
* Create action.
*
* @param \Symfony\Component\HttpFoundation\Request $request HTTP request
* @param \App\Repository\AnswerRepository $answerRepository Answer repository
*
* @return \Symfony\Component\HttpFoundation\Response HTTP response
*
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*
* @Route(
* "/create",
* methods={"GET", "POST"},
* name="answer_create",
* )
*/
public function create(Request $request, AnswerRepository $answerRepository): Response
{
$answer = new Answer();
$form = $this->createForm(AnswerType::class, $answer);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$answerRepository->save($answer);
$this->addFlash('success', 'answer_created_successfully');
return $this->redirectToRoute('answer_index');
}
return $this->render(
'answer/create.html.twig',
['form' => $form->createView(),
'question_id' => $answer, ]
);
}
And template with form:
{% extends 'base.html.twig' %}
{% block title %}
{{ 'title_answer_create'|trans ({'%id%': answer.id|default('')})}}
{% endblock %}
{% block body %}
<h1>{{ 'title_answer_create'|trans }}</h1>
{{ form_start(form, { method: 'POST', action: url('answer_create') }) }}
{{ form_widget(form) }}
<div class="form-group row float-sm-right">
<input type="submit" value="{{ 'action_save'|trans }}" class="btn btn-primary" />
</div>
<div class="form-group row float-sm-left">
<a href="{{ url('question_index') }}" class="btn btn-link">
{{ 'action_back_to_list'|trans }}
</a>
</div>
{{ form_end(form) }}
{% endblock %}