symfony用户访问网页的权限

时间:2017-05-04 08:56:44

标签: php symfony twig webpage

如果用户在其数据库中有特定值$row['GameId'];

,我想在网页上为某个网页制作一个特定的树枝输入表单

所以我试着写代码的if语句

  $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(),
     ));        

    //}

}

但我收到此错误

Notice: Undefined variable: form

因为这行

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

那么如何以正确的方式保护这个页面?

2 个答案:

答案 0 :(得分:0)

$ form仅在if(true);

中可用

下面的代码返回:注意:未定义的变量:a

<?php

    if("2"=="1")
    {
        $a = "10";
    }
    echo $a;

?>

在if条件之外定义$ form。

答案 1 :(得分:0)

Firstable,你需要从while循环块到外面,看看

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

第二名:
你的代码可以返回0,1或多种形式,

while ($row = $sqlo->fetch(PDO::FETCH_ASSOC))
  {
    $gid = $row['GameId'];

     if($gid == 0 ){$form = $this->createForm(new TaskType(), $investor);
    /**/}

查看你创建表单的每个循环指令,这样你就不知道最后会有多少表单,因为我们不知道有多少($ gid == 0)。
所以我建议,你将这些形式全部呈现给你的观点:

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

通过这种方式,您将所有表单推送到一个数组中,然后您需要做的就是将数组解析为您的twig文件:

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

希望我能提供帮助。