Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 redirect route

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()?

like image 647
pigfox Avatar asked Apr 07 '14 23:04

pigfox


2 Answers

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);
like image 92
Gara Avatar answered Oct 11 '22 21:10

Gara


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'));
} 
like image 36
kix Avatar answered Oct 11 '22 22:10

kix