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 %}
CodePudding user response:
Why dont you use it like this?
/**
* 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/{id}",
* methods={"GET", "POST"},
* name="answer_create",
* )
* @Entity("question", expr="repository.find(id)")
*/
public function create(Question $question, Request $request, AnswerRepository $answerRepository): Response
{
$answer = new Answer();
$answer->setQuestion($question);
$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, ]
if possible - you should name the parameter question_id
to make it clear what it is.
CodePudding user response:
Check the Documentation as to what version of symfony you are using, but you can access query parameters via the $request
object in your controller.
$question_id = $request->query->get('id');