dszpyf4859 2017-05-04 08:56
浏览 26
已采纳

symfony用户访问网页的权限

I want to make a certain twig input form on a web page available to user if he have a certain value in her database $row['GameId'];

so I tried to write if statement for the code like that

  $sqlo = $conn->query("SELECT * FROM user WHERE username='$tuser'");
  while ($row = $sqlo->fetch(PDO::FETCH_ASSOC))
  {
    $gid = $row['GameId'];

     if($gid == 0 ){
        $investor = $this->getDoctrine()->getRepository('AppBundle:User')->findOneBy(array('id' => $user->getId()));
        $MatchP = $investor->getMatchP();
        $form = $this->createForm(new TaskType(), $investor);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
             if(!empty($form->get('MatchP')->getData())){
                 $investor->setMatchP($form->get('MatchP')->getData());
             }
             else{
                 $investor->setMatchP($MatchP);
             }

             $em = $this->getDoctrine()->getManager();
             $em->persist($investor);
             $em->flush();

             $session = $this->getRequest()->getSession();


             /**
              * @Route("/siteblog_homepage/")
              */
            return $this->redirectToRoute('siteblog_homepage');
           }


     }
     else{

                ?> your value are not the right one to access to this page      <?

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

    //}

}

but i get this error

Notice: Undefined variable: form

because this line

     'form' => $form->createView(),

so how to protect this page on the wright way ?

  • 写回答

2条回答 默认 最新

  • dongsiju1941 2017-05-04 21:26
    关注

    Firstable, you need to get outside from the while loop block, take a look :

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

    Second :
    You'r code can return 0 ,1 or many forms ,

    while ($row = $sqlo->fetch(PDO::FETCH_ASSOC))
      {
        $gid = $row['GameId'];
    
         if($gid == 0 ){$form = $this->createForm(new TaskType(), $investor);
        /**/}
    

    Take a look for every loop instruction you create a form so you don't know how many form you gonna build at the end because we dont know how many ($gid == 0 ) .
    So i suggest to you that you render these all form to your view :

    $forms = array();
    array_push($forms,$form->createView());
    return $this->render('siteblogBundle:Default:index.html.twig', array(
             'forms' => $forms));
    

    By that way you pushed all your form into an array ,then all you need to do is to parse the array into your twig file:

    {% for form in forms %}
        {{dump(form)}}
        {{form(form)}}
      {% endfor %}
    

    wish i was helpful .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)