Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to access an entity in twig from symfony2. Inside the controler, I can do something as:

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

And then in twig I can access the entity properties with entity.name and such.

I need to accomplish the same thing but with the function redirectToRoute()

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

But it doesn't seem to work.

I'm getting the following error:

Variable "entity" does not exist in frontendBundle::carrodecompras.html.twig at line 32

EDIT: I'm using Symfony 2.7

The variable $entity exists (it's actually called $cortina in the app I was using $entity for simplification), just before the redirectToRoute function I did this to test it

echo "<pre>";
var_dump($cortina);
echo "</pre>";

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

And the result is this:

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>
like image 308
enlego Avatar asked Jan 13 '16 19:01

enlego


2 Answers

When you call redirectToRoute($route, array $parameters) from a controller, $parameters is used to generate the url tokens, not variables to render in view, 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));
    }
}
like image 71
Heah Avatar answered Oct 04 '22 18:10

Heah


$this->redirectToRoute('something', array('id' => 1) is a convenience wrapper to $this->redirect($this->generateUrl('something', array('id' => 1))). It builds a URL with your params and is expecting the value of the params 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 id of the entity to then fetch the data in the new action or break it down into individual pieces of data before it hits 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()
        );
    }
}
like image 23
SacWebDeveloper Avatar answered Oct 04 '22 17:10

SacWebDeveloper