Can the redirectToRoute () method have arguments like render ()?

I need to access an object in twig from symfony2. Inside the controller, I can do something like:

return $this->render('frontendBundle::carrodecompras.html.twig', array( 'entity' => $entity )); 

And then in twig, I can access the properties of an entity using entity.name , etc.

I need to do the same thing, but using the redirectToRoute() function

 return $this->redirectToRoute('frontend_carrodecompras', array( 'entity' => $entity, )); 

But that does not work.

I get the following error:

The entity variable does not exist in frontendBundle :: carrodecompras.html.twig on line 32

EDIT: I am using Symfony 2.7

The existing $ entity variable exists (it is actually called $ cortina in the application in which I used $ entity to simplify), just before the redirectToRoute function, I did this to check it

 echo "<pre>"; var_dump($cortina); echo "</pre>"; return $this->redirectToRoute('frontend_carrodecompras', array( 'cortina' => $cortina, )); 

And the result is the following:

 object(dexter\backendBundle\Entity\cortina)#373 (16) { ["id":"dexter\backendBundle\Entity\cortina":private]=> int(3) ... 

This is the Twig code:

 <tr> {% set imagentela = "img/telas/" ~ cortina.codInterno ~ ".jpg" %} <td><img src="{{ asset(imagentela | lower ) }}" alt="" width="25" height="25"> </td> <td>{{ cortina.nombre }}</td> <td>{{ "$" ~ cortina.precio|number_format('0',',','.') }}</td> </tr> 
+5
source share
2 answers

When you call redirectToRoute($route, array $parameters) from the controller, $parameters used to generate url tokens, and not to render the visualization, this is done by the controller assigned to the route you are redirecting to.

example:

 class FirstController extends Controller { /** * @Route('/some_path') */ public function someAction() { // ... some logic $entity = 'some_value'; return $this->redirectToRoute('some_other_route', array('entity' => $entity)); // cast $entity to string } } class SecondController extends Controller { /** * @Route('/some_other_path/{entity}', name="some_other_route") */ public function otherAction($entity) { // some other logic // in this case $entity equals 'some_value' $real_entity = $this->get('some_service')->get($entity); return $this->render('view', array('twig_entity' => $real_entity)); } } 
+11
source

$this->redirectToRoute('something', array('id' => 1) is a convenient wrapper for $this->redirect($this->generateUrl('something', array('id' => 1))) . It creates a URL with your parameters and expects the params value to be a string or a number.

http://symfony.com/blog/new-in-symfony-2-6-new-shortcut-methods-for-controllers

You need to either pass the object identifier to then retrieve the data in the new action or split it into separate pieces of data before it calls the redirectToRoute () call.

 class MyController extends Controller { public function myAction(){ $cortina = new Cortina(); $cortina->something = "Some text"; $em = $this->getDoctrine()->getManager(); $em->persist($cortina); $em->flush(); return $this->redirectToRoute('frontend_carrodecompras', array( 'id' => $cortina->getId() ); } } 
+1
source

Source: https://habr.com/ru/post/1240566/


All Articles