【问题标题】:How to get an id from url in Symfony [duplicate]如何从 Symfony 中的 url 获取 id [重复]
【发布时间】:2021-11-05 15:50:12
【问题描述】:

所以我有一个这样的网址: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 %}

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    你为什么不这样用呢?

    /**
     * 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 以明确它是什么。

    【讨论】:

      【解决方案2】:

      检查Documentation 以了解您使用的是什么版本的symfony,但您可以通过控制器中的$request 对象访问查询参数。

      $question_id = $request->query->get('id');
      

      【讨论】:

      • -&gt;query 不需要。
      猜你喜欢
      • 2023-03-24
      • 2018-10-28
      • 2023-03-27
      • 2015-09-07
      • 2016-10-25
      • 1970-01-01
      • 2019-02-20
      • 2014-02-06
      • 2014-01-12
      相关资源
      最近更新 更多