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

报告相同问题?

悬赏问题

  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化