将数据从Twig传递到控制器时出错

时间:2019-05-21 11:30:40

标签: symfony doctrine-orm doctrine symfony4

我正在尝试将一些数据从树枝传递到控制器,但出现此错误

No route found for "POST /agent/": Method Not Allowed (Allow: GET)

让我解释一下我做了什么,我有一个列表,并且列表中的每辆车都有一个按钮,我单击该按钮以显示一个模态表格,该表格使我可以创建与该车有关的车票。

按下该按钮会将所选汽车的ID和编号传递到表单。我能够在不传递任何内容的情况下呈现表单,但是每当尝试传递汽车ID时,我都会收到错误消息。

我的代码: index.html.twig

      {% for parking in user.parkings %}

         <table id="file_export" class="table table-striped table-bordered">
                <tbody>
            {% for car in car %}
                <tr>
                <td>
               {{ car.matricule }}
                </td>
                <td>
              <span class="timer" data-expires="{{ car.getExpiresAt() }}"> 
              </span>

                </td>
             <td>

<button type="button" class="btn btn-dark" href="{{ path('new_amende', {'id': car.id},{'number': car.number}) }}" data-toggle="modal" data-target="#createmodel" data-whatever="{{ car.id }}">
ticket                                
</button>
            {{render(controller('App\\Controller\\AgentController:newAmende')) }}

                </td>
                </tr>
             {% endfor %}

控制器

    /**
     * @Route("/{id}/new", name="new_ticket", methods={"GET","POST"})
     */
    public function newTicket(Request $request, Car $car): Response
    {
        $ticket = new Ticket();
        $form = $this->createForm(TicketType::class, $ticket);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->addFlash('success','ticket added !');

            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($ticket);
            $entityManager->flush();

            return $this->redirectToRoute('agent');


         }

        return $this->render('Agent/modal.html.twig', [
            'ticket' => $ticket,
            'form' => $form->createView(),
        ]);
    }

编辑,这是我的另一个控制器


    /**
     * @Route("/", name="agent", methods={"GET"})
     */
    public function index(): Response
    {

        $use = $this->get('security.token_storage')->getToken()->getUser();

        $user = $this->getUser();   
        $parkingz=$this->getUser()->getParkings();

        return $this->render('Agent/Agent.html.twig', [
            'user' => $user,
            'parkings'=>$parkings,
        ]);
    }

1 个答案:

答案 0 :(得分:1)

您正尝试使用POST到达/ agent,而您只允许在此路由No route found for "POST /agent/": Method Not Allowed (Allow: GET)上执行GET请求。

因此修改您的index()以允许POST:

 /**
 * @Route("/", name="agent", methods={"GET","POST"})
 */
public function index(): Response
{