Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between $this->render and $this->redirect Symfony2

Tags:

php

symfony

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

like image 956
ScoRpion Avatar asked Feb 25 '12 08:02

ScoRpion


1 Answers

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.

like image 88
Hades Avatar answered Nov 05 '22 11:11

Hades