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

Sco*_*ion 8 php symfony

$ this-> render和$ this-> redirect之间有什么区别.有没有办法可以使用$ this-> render传递参数,就像我使用$ this-> redirect一样

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

我可以这样做: -

return $this->redirect($this->generateUrl('MedicineUserBundle_login', array(
             'info'      => $all,)));
Run Code Online (Sandbox Code Playgroud)

或者有没有其他方法我可以使用$ this-> redirect传递值到我的模板twig文件.

还有一个问题我如何更改网址$this->redirect,例如,如果我不必将任何值传递给我的模板文件,我可以像上面提到的那样,渲染会将我带到像localhost/myproject/home这样的页面,但$->this->redirect会执行控制器但是url会像localhost/myproject /一样.无论如何我可以使用重定向重定向到另一个网址

Had*_*des 32

重定向()

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

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

渲染()

渲染只是将您告诉它的模板文件呈现为对当前请求的响应.有了这个,您可以正常传递模板参数数组.

向前()

还有Forward将请求转发给另一个控制器,在内部发送该控制器响应作为对当前请求的响应而没有任何重定向.使用此方法在内部重新路由请求,而不更改地址栏中的URL.

Render和Redirect之间的关键区别在于Render是View系统的一部分,因此可以将参数传递给tempaltes.Redirect是Controller系统的一部分,对View没有任何了解.您可以将参数传递给要重定向到的路径或URL,但目标位置必须对它们执行某些操作才能将它们传递给View.