doupaoshu8334 2012-02-25 08:26
浏览 53
已采纳

$ this-> render和$ this->重定向Symfony2之间的区别

What is the difference between $this->render and $this->redirect. Is there a way i can pass arguments with $this->render like in I do with $this->redirect

return $this->render('MedicineUserBundle:User:home.html.twig', array(
                 'info'      => $all,));

Can I do something like this :-

return $this->redirect($this->generateUrl('MedicineUserBundle_login', array(
             'info'      => $all,)));

Or is there any other way I can pass values with $this->redirect to my template twig file.

And One more question How can i change the url with $this->redirect, eg If i dont have to pass any values to my template file I can do as mentioned above the render will take me to a page like localhost/myproject/home but $->this->redirect will execute the controller but the url wil be the same like localhost/myproject/. Is there anyway i can redirect to another url using redirect

  • 写回答

1条回答 默认 最新

  • doujindou4356 2012-02-25 12:10
    关注

    Redirect()

    Redirect performs a 301 or 302 redirect to the specified route/location. You can use this to pass in a full URL I believe. Using this method will cause the URL to change in the address bar.

    Because Redirect uses a simple 301/302 header to do the redirect there is no way to pass template parameters to the new location except for on the URL as you would do to any controller or URL.

    Render()

    Render just renders up the template file you tell it to as a response to the current request. With this you can pass in your array of template parameters as normal.

    Forward()

    There is also Forward which will forward the request to another controller internally sending that controllers response back as the response to the current request without any redirects. Using this method re-routes the request internally without changing the URL in the address bar.

    The key difference here between Render and Redirect is that Render is part of the View system and therefore can pass parameters to the tempaltes. Redirect is part of the Controller system and doesn't know anything about the View. You can pass parameters to the route or URL you are redirecting to but the target location must then do something with them to pass them on to the View.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺