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

报告相同问题?

悬赏问题

  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader
  • ¥45 谷歌浏览器出现开发者工具无法显示已创建的,但您可以调试已部署的代码。 状态代码 404, net::ERR HTTP RESPONSE CODE FAILURE
  • ¥15 chatgpt网页版侧边栏和模型切换点击没反应怎么办
  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas