简体   繁体   English

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

[英]How to pass a form variable from a controller action to another in Symfony 3

I'm using Symfony 3 trying to develop a website that manage students absence. 我正在使用Symfony 3尝试开发一个管理学生缺勤的网站。 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 : 在第一个动作中,我将创建一个表单,用户将在其中选择学生姓名,然后我恢复提交的值以获取该学生的ID,并且我想将此ID传递给第二个动作,在第二个动作中,我将使用id并将其显示在树枝页面中:

/**
* @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 这是我尝试使用上一个Action的ID来获取缺席的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. 我在实体缺勤和学生之间有一个ManyToOne关系,因此我在缺勤实体中有一个名为Student的字段,这就是为什么我在findBy()方法中使用Student字段。

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 问题是在listabsencestudentshowAction()中无法识别变量$ id,我不知道如何将此形式变量从一个动作传递到另一个动作

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));

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Symfony中将变量从控制器传递到另一个? - Pass variable from controller to another in Symfony? 如何将变量从preDispatch传递给控制器​​和动作? - how to pass a variable from preDispatch to controller and action? 如何将变量从动作传递到形式 - How to pass variable from action to form 如何将对象从symfony控制器传递到angularjs,以便使用它定义变量? - How to pass an object from symfony controller to angularjs in order to define a variable with it? 如何将变量从javascript传递到symfony2控制器 - How to pass a variable from javascript to symfony2 controller 如何在控制器动作Symfony 2中获取表格 - How get form in controller action Symfony 2 如何使用Codeigniter通过表单操作在控制器中传递ID - how to pass id in controller from form action using codeigniter Symfony2将变量从控制器传递到javascript - Symfony2 pass variable from controller to javascript 如何在Laravel中将变量从一个控制器传递到另一个控制器 - How to pass variable from one controller to another controller in laravel 如何通过 POST 将数据从一个控制器操作传递到另一个控制器操作 - How to pass data from one controller action to another via POST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM