简体   繁体   English

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

[英]Difference between $this->render and $this->redirect Symfony2

What is the difference between $this->render and $this->redirect. $ this-> render和$ this-> redirect之间有什么区别。 Is there a way i can pass arguments with $this->render like in I do with $this->redirect 有没有办法可以使用$ this-> render传递参数,就像我使用$ 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. 或者有没有其他方法我可以使用$ this-> redirect传递值到我的模板twig文件。

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/ . 还有一个问题我如何使用$this->redirect更改网址,例如,如果我不必将任何值传递给我的模板文件,我可以像上面提到的那样将渲染带到像localhost / myproject / home这样的页面但$->this->redirect将执行控制器,但url将与localhost / myproject /相同。 Is there anyway i can redirect to another url using redirect 无论如何我可以使用重定向重定向到另一个网址

Redirect() 重定向()

Redirect performs a 301 or 302 redirect to the specified route/location. 重定向执行301或302重定向到指定的路由/位置。 You can use this to pass in a full URL I believe. 您可以使用它来传递我认为的完整URL。 Using this method will cause the URL to change in the address bar. 使用此方法将导致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. 由于Redirect使用简单的301/302标头进行重定向,因此除了URL之外,无法将模板参数传递到新位置,就像对任何控制器或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. 还有Forward将请求转发给另一个控制器,在内部发送该控制器响应作为对当前请求的响应而没有任何重定向。 Using this method re-routes the request internally without changing the URL in the address bar. 使用此方法在内部重新路由请求,而不更改地址栏中的URL。

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. Render和Redirect之间的关键区别在于Render是View系统的一部分,因此可以将参数传递给tempaltes。 Redirect is part of the Controller system and doesn't know anything about the View. Redirect是Controller系统的一部分,对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. 您可以将参数传递给要重定向到的路径或URL,但目标位置必须对它们执行某些操作才能将它们传递给View。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM