Reputation: 5158
$controller->forward(...)
requires a string in form or MyBundle:Controller:action
, but I only have route name.
How to forward request to route name?
Upvotes: 8
Views: 13934
Reputation: 1
Beware of
router->getRouteCollection()
It is suggested in the accepted answer, but using this method will regenerate the Routing cache on each request, making apps very slow.
https://github.com/symfony/symfony-docs/issues/6710
Upvotes: 0
Reputation: 83
Redirect to route name 'homepage':
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class TaskController extends AbstractController
{
public function goHome()
{
return $this->redirectToRoute('homepage');
}
}
Upvotes: 5
Reputation: 3284
this question is old but for the sake of documentation and for all the googlers out there here is one way of doing it.
class Mycontroller extends Controller {
public function indexAction(){
$response = $this->forward($this->routeToControllerName('ROUTE_NAME_HERE'));
}
private function routeToControllerName($routename) {
$routes = $this->get('router')->getRouteCollection();
return $routes->get($routename)->getDefaults()['_controller'];
}
}
Upvotes: 12
Reputation: 570
Forward to controller.
Redirect to a page (by route)
http://symfony.com/doc/current/book/controller.html#redirecting
Upvotes: 1