如何从 Symfony 中的 url 获取 id [重复]

Posted

技术标签:

【中文标题】如何从 Symfony 中的 url 获取 id [重复]【英文标题】:How to get an id from url in Symfony [duplicate] 【发布时间】: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 %

【问题讨论】:

【参考方案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 不需要。

以上是关于如何从 Symfony 中的 url 获取 id [重复]的主要内容,如果未能解决你的问题,请参考以下文章

从 Symfony 控制器中的值获取 Id

Symfony2 Assetic 从控制器内部获取资产 url,而不是模板

尝试获取关系中的数据时出现 Symfony 错误

如何从javascript中的url获取id? [复制]

如何使用教义/symfony4 从数据库中获取(连接)两条记录

为所有从包路由导入的前缀添加前缀时,如何在 Symfony 包的模板中生成 URL?