Symfony 2 redirect route

54,451

Solution 1

Clear your cache using php app/console cache:clear

return $this->redirect($this->generateUrl('CanopyAbcBundle_crud_success'));

If parameters are required pass like this:

return $this->redirect($this->generateUrl('CanopyAbcBundle_crud_success', array('param1' => $param1)), 301);

Solution 2

The first line of your YAML is the route name that should be used with the router component. You're trying to generate a URL for the wrong route name, yours is CanopyAbcBundle_crud_success, not crud_success. Also, generateUrl() method does what it says: it generates a URL from route name and parameters (it they are passed). To return a 403 redirect response, you could either use $this->redirect($this->generateUrl('CanopyAbcBundle_crud_success')) which is built into the Controller base class, or you could return an instance of Symfony\Component\HttpFoundation\RedirectResponse like this:

public function yourAction()
{
    return new RedirectResponse($this->generateUrl('CanopyAbcBundle_crud_success'));
} 
Share:
54,451
pigfox
Author by

pigfox

I solve your problems and make it work...

Updated on April 01, 2020

Comments

  • pigfox
    pigfox about 4 years

    I have the following route that works via a get:

    CanopyAbcBundle_crud_success:
      pattern:  /crud/success/
      defaults: { _controller: CanopyAbcBundle:Crud:success }
      requirements:
        _method:  GET
    

    Where Canopy is the namespace, the bundle is AbcBundle, controller Crud, action is success.

    The following fails:

    return $this->redirect($this->generateUrl('crud_success'));
    
    Unable to generate a URL for the named route "crud_success" as such route does not exist.
    500 Internal Server Error - RouteNotFoundException 
    

    How can I redirect with generateUrl()?