我有身份验证控制器
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)));
}
如果用户提供了正确的登录名和密码,他将被重定向到加入树枝,在这个树枝中,我想让用户的id在另一个树枝中使用它,所以我遇到问题的一行是这样:
<a href="{{ path('mql14mqlme_interet', { 'id': name }) }}">INTERETS</a>
我得到的错误是:变量&#34;名称&#34;在Mql14mqlmeBundle中不存在:默认:第89行的acceuil.html.twig
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
));
}
答案 0 :(得分:2)
尝试:
<a href="{{ path('mql14mqlme_interet', { 'id': app.request.get('name') }) }}">INTERETS</a>
问题:
你的
$ user = $ repository-&gt; findOneBy(array('login'=&gt; $ username,'pass'=&gt; $ password));
表示您通过普通密码查找用户?
答案 1 :(得分:0)
在twig中,调用render时需要传入所有变量。 EG:
echo $template->render(array('name' => 'bob'));
答案 2 :(得分:0)
您需要从查询字符串中获取名称并将名称传递给渲染视图
public function acceuilAction($name) // <-- This is the name you are passing to request
{
...
return $this->container->get('templating')->renderResponse('Mql14mqlmeBundle:Default:acceuil.html.twig',
array(
'name' => $name, // <-- Here you pass the received name to your twig
'evenements' => $evenements,
'categories' => $categories
));
}
基于documentation生成URL并将参数作为数组传递,意味着您通过请求传递名称。如果要作为QueryString传递(不是url路径的一部分),请将生成URL更改为
$this->redirect($this->generateUrl('mql14mqlme_acceuil').'?name='.$user->getId());
在这种情况下,您还需要更改路由和控制器