The difference between $ this-> render and $ this-> redirect Symfony2

What is the difference between $ this-> render and $ this-> redirect. Is there a way that I can pass arguments using $ this-> render, as is the case 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 another way to pass values ​​using $ this-> redirect to my twig file.

And one more question. How to change the URL using $this->redirect , for example, if I don’t need to pass any values ​​to the template file that I can do as described above, the render will lead me to a page, for example localhost / myproject / home , but $->this->redirect will execute the controller, but the url will be the same as localhost / myproject / . Anyway, I can redirect to another url using redirect

+8
php symfony
source share
1 answer

Redirection ()

Redirection redirects 301 or 302 to the specified route / location. You can use this to pass the full URL that I consider. Using this method will change the URL in the address bar.

Since Redirect uses a simple 301/302 header to perform redirection, it is not possible to pass the template parameters to a new location other than the URL, as it would with any controller or URL.

Render ()

Render simply displays the template file that you specify in response to the current request. In this case, you can pass the template parameters to your array as usual.

Forward()

There is also Forward, which forwards the request to another controller, which sends this response back to the controllers as a response to the current request without any redirects. Using this method redirects the request internally without changing the URL in the address bar.

The key difference between Render and Redirect is that Render is part of the View system and therefore can pass tempaltes parameters. Redirection is part of the Controller system and knows nothing about the view. You can pass parameters to the route or URL you are redirecting to, but the destination location must then do something with them to pass them to the view.

+32
source share

All Articles