Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to forward request to route name

Tags:

symfony

$controller->forward(...) 

requires a string in form or MyBundle:Controller:action, but I only have route name.

How to forward request to route name?

like image 707
Zeljko Avatar asked Jul 05 '13 16:07

Zeljko


2 Answers

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'];
  }

}
like image 149
Josh Avatar answered Nov 14 '22 22:11

Josh


Redirect to route name 'homepage':


use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class TaskController extends AbstractController
{

   public function goHome()
   {
      return $this->redirectToRoute('homepage');
   }
}
like image 28
Gammerz Avatar answered Nov 14 '22 22:11

Gammerz