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

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


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));    }}


$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()        );    }}