3

I would like to basically get the same result as Twig's function path(routeName) but not in the view.html.twig but inside a controller itself.

routing.yml :

MyBundle_route_first:
pattern:  /dummy/first/{foo}
defaults: { _controller: MyBundle:Dummy:First }
requirements:
  _method:  GET
  foo: \d+

MyBundle_route_second:
pattern:  /dummy/second/{foo}/{bar}
defaults: { _controller: MyBundle:Dummy:Second }
requirements:
  _method:  GET
  foo: \d+
  bar: \d+

controller.php :

class DummyController extends Controller {

  public function firstAction($foo) {
    // do some stuff
  }
  public function secondAction($foo, $bar) {
    // do some stuff
  }

  public function anotherAction() {
    $firstRoutePattern = some_magic_function("MyBundle_route_first");
    // "/dummy/first/{foo}"

    $secondRoutePattern = some_magic_function("MyBundle_route_second");
    // "/dummy/second/{foo}/{bar}"

  }
}

Any help or link to a related topic would be much apreciated.

Have a good day :-)

Pierre Roudaut
  • 1,013
  • 1
  • 18
  • 32

1 Answers1

2

Look at this method : generate

$firstRoutePattern = $this->container->get('router')->generate('MyBundle_route_first', array('foo' => $foo));
Cruz
  • 695
  • 8
  • 21
  • Actually, the right syntax including the parameter to pass to the **generate** function was: `$firstRoutePattern = $this->container->get('router')->generate('MyBundle_route_first', array('foo' => $foo));` But you put me in the right direction ;-) – Pierre Roudaut Oct 28 '15 at 15:54
  • 1
    And since you are in a controller you might try: $this->generateUrl('MyBundle_route_first', array('foo' => $foo)); Lots of useful helper functions in the base controller class. – Cerad Oct 28 '15 at 15:58
  • Thanks @Cerad, I'll check it out. I'm still learning this huge framework ! – Pierre Roudaut Oct 28 '15 at 16:00
  • Controller shortcut are nice but I think it's important to know the original method as it can be useful for service definition. – Cruz Oct 28 '15 at 16:03