$ this-> render和$ this-> redirect Symfony2之间的区别 [英] Difference between $this->render and $this->redirect Symfony2

查看:94
本文介绍了$ this-> render和$ this-> redirect Symfony2之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ this-> render和$ this-> redirect之间有什么区别.有没有一种方法可以像我对$ this-> redirect那样在$ this-> render中传递参数

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,)));

或者还有其他方法可以通过$ this->重定向值传递到模板树枝文件.

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

还有一个问题,如何使用$this->redirect更改url,例如,如果我不必将任何值传递给模板文件,我就可以按照上面提到的方式进行操作,渲染将带我到 localhost之类的页面/myproject/home ,但$->this->redirect将执行控制器,但url与 localhost/myproject/相同.无论如何,我可以使用redirect重定向到另一个URL

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

推荐答案

Redirect()

重定向执行301或302重定向到指定的路由/位置.我相信您可以使用它来传递完整的URL.使用此方法将导致URL在地址栏中更改.

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.

由于重定向使用简单的301/302标头进行重定向,因此无法像在任何控制器或URL上一样将模板参数传递到新位置,除了URL上.

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(转发),它将转发请求到另一个控制器,该控制器在内部将该控制器的响应作为对当前请求的响应发送回去,而无需任何重定向.使用此方法可以在内部重新路由请求,而无需更改地址栏中的URL.

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.

Render和Redirect之间的主要区别在于Render是View系统的一部分,因此可以将参数传递给模板.重定向是Controller系统的一部分,对View一无所知.您可以将参数传递给您要重定向到的路由或URL,但是目标位置必须对它们进行一些操作才能将其传递给View.

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.

这篇关于$ this-> render和$ this-> redirect Symfony2之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆