如何在 Symfony 中从 url 获取一个 id
How to get an id from url in Symfony
所以我有一个像这样的 url:localhost:8000/answer/create?id=254.
完整的获取方式是:localhost:8000/questions -> localhost:8000/questions/{question_id} -> localhost:8000/answer/create? id=254.
如何让应用读取这个 id=254 作为 question_id?
部分控制器:
/**
* 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, ]
);
}
以及表单模板:
{% 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 %}
检查 Documentation 您使用的是什么版本的 symfony,但您可以通过控制器中的 $request
对象访问查询参数。
$question_id = $request->query->get('id');
为什么不这样用呢?
/**
* 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, ]
如果可能 - 您应该将参数命名为 question_id
以明确它是什么。
所以我有一个像这样的 url:localhost:8000/answer/create?id=254.
完整的获取方式是:localhost:8000/questions -> localhost:8000/questions/{question_id} -> localhost:8000/answer/create? id=254.
如何让应用读取这个 id=254 作为 question_id?
部分控制器:
/**
* 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, ]
);
}
以及表单模板:
{% 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 %}
检查 Documentation 您使用的是什么版本的 symfony,但您可以通过控制器中的 $request
对象访问查询参数。
$question_id = $request->query->get('id');
为什么不这样用呢?
/**
* 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, ]
如果可能 - 您应该将参数命名为 question_id
以明确它是什么。