开发者

Get routing requirement variable in Symfony2

in order to create a navigation for me webinterface I'd like to get a variable from the routing config of my bundle. I define the available pages in mybundle/Resources/config/routing.yml.

mybundle_homepage:
  pattern:  /{_locale}/{branch}/{page}
  defaults: { _controller: mybundle:mycontroller:index, _locale: de, bra开发者_如何学JAVAnch: x.x.x, page: start }
  requirements:
    _locale:  de|en
    page:     start|functions|events|constants|styleguide

Now I had a look at the Symfony2 YAML Parser and I have to provide a filepath to it's static method parse: http://symfony.com/doc/2.0/reference/YAML.html

mycontroller.php

use Symfony\Component\Yaml\Yaml;

class mycontroller extends Controller
{
  public function indexAction($_locale, $branch, $page)
  {
    $routing = Yaml::parse('../Resources/config/routing.yml');
    var_dump($routing);
  }
}

I thought I could do it that way because the folder hirarchy looks like that:

  • mybundle
    • Controller
      • mycontroller.php
    • Rescources
      • config
        • routing.yml

But it's not working. Any ideas or maybe another way to get the requirements.page array from the routing file?

Regards, Ben


You should be able to access the router service inside a class that's DI container aware. So, you can write something like:

$routes = $this->container->get('router')->getRouteCollection();

$route = $routes->get('my_route_name');
print_r($route->getRequirements());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜