donglu9825 2014-06-13 19:26
浏览 182
已采纳

在twig中获取URL变量

I have the authentification controller

public function indexAction(Request $request)
{

$em = $this->getDoctrine()->getEntityManager();
$repository = $em->getRepository('Mql14mqlmeBundle:User');

   if ('POST' === $request->getMethod()) {
        $username = $request->get('login');
    $password = $request->get('pass');
          if(($username=='admin')&&($password=='admin')){

           return $this->redirect($this->generateUrl('mql14mqlme_admin',  array('name' => 'welcome')));
            }

    $user = $repository->findOneBy(array('login' => $username, 'pass' =>  $password));

    if ($user) {

        return $this->redirect($this->generateUrl('mql14mqlme_acceuil', array('name' => $user->getNom(),
                'id'=> $user->getId(),
            )));


    }else {
      return $this->redirect($this->generateUrl('mql14mqlme_homepage', array('name' =>$username)));
  }

If the user has provided the right login and password he is redirected to acceuil twig, in this twig I want to get the user's id to use it in another twig, so the line where I have a problem is this:

<a href="{{ path('mql14mqlme_interet', { 'id': name   }) }}">INTERETS</a>

the error I get is:Variable "name" does not exist in Mql14mqlmeBundle:Default:acceuil.html.twig at line 89

The action code for acceuil:

public function acceuilAction()
{

$em = $this->container->get('doctrine')->getEntityManager();
$evenements= $em->getRepository('Mql14mqlmeBundle:Evenement')->findAll();
$categories= $em->getRepository('Mql14mqlmeBundle:Categorie')->findAll();
return $this->container->get('templating')->renderResponse('Mql14mqlmeBundle:Default:acceuil.html.twig',
        array(
                'evenements' => $evenements,
                'categories' => $categories
        ));

}   
  • 写回答

3条回答 默认 最新

  • dtj4307 2014-06-13 19:59
    关注

    Try :

    <a href="{{ path('mql14mqlme_interet', { 'id': app.request.get('name') }) }}">INTERETS</a>
    

    Question :

    Does your

    $user = $repository->findOneBy(array('login' => $username, 'pass' => $password));

    indicates that your looking for user by plain password ?

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

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画