如何在Symfony 3中将表单变量从控制器动作传递给另一个

问题描述:

I'm using Symfony 3 trying to develop a website that manage students absence. When a user want to see absences he must give a student name so that he can see absences for this specific student. The user gives the student name in a form ( combobox containing a list of students)

I'm trying to use this form variable ( the name) to display absences for the student with this name.

This is my code.

In this first action i'm creating the form where the user will chose the student name and i recover the submitted value to get the ID of the student and i want to pass this id to the second action where i will fetch the absences using the id and display it in the twig page :

/**
* @Route("/absence/list/bystudent", name = "list_absence_student")
*/

public function listabsencestudentAction(Request $request)
{
    $absence = new Absence();
    $form   = $this->get('form.factory')->create(AbsenceStudentType::class, $absence);


    if($request->getMethod() == 'POST'){
        $form->handleRequest($request);
        $data = $form->getData();
    }
    if($form->isValid()){
        $id = $data->getStudent()->getID();

        return $this->redirectToRoute('list_absence_student_show',array('id', $id));
    }

    return $this->render('AbsenceBundle:Default:listabsencestudent.html.twig', array('f'=> $form->createView()));
}

And this is the Action where i'm trying to fetch the absences using the id of the previous Action

 /**
  * @Route("/absence/list/bystudent/show", name = "list_absence_student_show")
 */

public function listabsencestudentshowAction()
{
 $absences = $this->getDoctrine()->getRepository("AbsenceBundle:Absence")->findBy('student' => $id);
           return $this->render('AbsenceBundle:Default:listabsencestudentshow.html.twig', array('absences' => $absences));
}

I have a relation ManyToOne between the entities Absence and Student so i have a field named student in my Absence entity that why im using the student field in the findBy() Method.

The problem is that the variable $id is not recognize in the listabsencestudentshowAction() i don't know how to pass this form variable from an action to another

Can anyone help me with this please.

I found a solution to my problem & i want to share it maybe someone will need it

in order to recover the attributes that i passed i needed to add a slug in the rout of the second controller action then add arguments to this action

this is the code

nothing changed in the first Action

/**
* @Route("/absence/list/bystudent", name = "list_absence_student")
*/

public function listabsencestudentAction(Request $request)
{
    $absence = new Absence();
    $form   = $this->get('form.factory')->create(AbsenceStudentType::class, $absence);


if($request->getMethod() == 'POST'){
    $form->handleRequest($request);
    $data = $form->getData();
}
if($form->isValid()){
    $id = $data->getStudent()->getID();

    return $this->redirectToRoute('list_absence_student_show',array('id', $id));
}

return $this->render('AbsenceBundle:Default:listabsencestudent.html.twig', array('f'=> $form->createView()));

}

And this is the second bundle

/**
 * @Route("/absence/list/bystudent/show/{id}", name = "list_absence_student_show")
 */

public function listabsencestudentshowAction(Request $request, $id)
{
    $absences = $this->getDoctrine()->getRepository("AbsenceBundle:Absence")->findBy(array('student' => $id));
    return $this->render('AbsenceBundle:Default:listabsencestudentshow.html.twig', array('absences' => $absences, 'id' => $id));

}