简体   繁体   中英

How to retrieve a route pattern from a route name in a Symfony Controller

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 :-)

看看这个方法: 生成

$firstRoutePattern = $this->container->get('router')->generate('MyBundle_route_first', array('foo' => $foo));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM