2012-01-07 79 views
1

时如何发送数据我从数据库中获取一个对象,然后我想打一个重定向到另一个控制器和发送那里对象:symfony的2重定向

$user = $this->getDoctrine()->getRepository('ShopMyShopBundle:Register')->find($id); 

return $this->redirect($this->generateUrl('ShopMyShopBundle_homepage'), array('user' => $user)); 

我怎样才能得到后一个对象重定向并发送到其他控制器功能中的模板,如下所示:

public function indexAction() 
{ 
    return $this->render('ShopMyShopBundle:Main:index.html.twig'); 
} 

回答

3

您无法通过重定向发送对象。你可以做的是发送它的一个独特的属性(可能是主键)并再次从数据库中获取它。在你的情况下,它可能是一个用户ID或昵称。

/** 
* @Route("/shop/{nickname}", name="shop_index") 
*/ 
public function indexAction(User $user) 
{ 
    // ... 
} 

得益于类型提示时,用户将被从数据库automatically通过其昵称获取。

+0

如何将$ user变量发送到模板。我有&user = $ this-> getDoctrine() - > getRepository('ShopMyShopBundle:Register') - > find($ id);然后我想这样做:return $ this-> render('ShopMyShopBundle:Main:register.html.twig',array('form'=> $ form-> createView(),'user'=> $用户));但我得到一个错误“注意:未定义的变量:用户在...” – user1052836 2012-01-07 19:34:48

+0

这需要SensioFrameworkExtraBundle,顺便说一句。 – 2012-01-08 04:07:50

+0

我发现了一个错误,也许你错了理解我。我忘了使用EntityManager。现在我有:$ em = $ this-> getDoctrine() - > getEntityManager(); $ user = $ em-> getRepository('ShopMyShopBundle:Register') - > find($ id);返回$ this-> render('ShopMyShopBundle:Main:register.html.twig',array('form'=> $ form-> createView(),'user'=> $ user));并在模板中可以使用{{user.login}}; – user1052836 2012-01-08 15:24:11