Symfony Custom Route loader,加载多次,获得异常

时间:2014-07-16 07:26:19

标签: symfony url-routing

我正在尝试根据http://symfony.com/doc/current/cookbook/routing/custom_route_loader.html

制作自定义路由器

我的代码看起来像这样

//the routeloader:

//the namespace and use code ....

class FooLoader extends Loader{
  private $loaded = false;
  private $service;

  public function __construct($service){
    $this->service = $service;
  }
  public function load($resource, $type=null){
      if (true === $this->loaded) 
        throw new \RuntimeException('xmlRouteLoader is already loaded');
      //process some routes and make $routeCollection
      $this->loaded = true;
      return $routeCollection;
  }
      public function getResolver()
     {
    // needed, but can be blank, unless you want to load other resources
    // and if you do, using the Loader base class is easier (see below)
     }

    public function setResolver(LoaderResolverInterface $resolver)
    {
    // same as above
    }
    function supports($resource, $type = null){

         return $type === 'xmlmenu';
    }

}

//the service definition
    foo.xml_router:
    class: "%route_loader.class%"
    arguments: [@foo.bar_service] //this service and the injection has been tested and works.
    tags:
        - { name: routing.loader }

//the routing definitions

//routing_dev.yml
_foo: 
  resource: "@FooBarBundle/Resources/config/routing.yml"

-----------------------------
//FooBarBundle/Resources/config/routing.yml
_xml_routes:
  resource: .
  type: xmlmenu

当我尝试访问任何路线时,我得到了例外:

  

RuntimeException:已加载xmlRouteLoader

这是我在多次加载加载器时定义的异常。那为什么它会尝试不止一次加载这个加载器?而且我很确定我只在那里定义了它。

1 个答案:

答案 0 :(得分:0)

实际上答案很简单。看起来这个方法只支持一个级别的imports.I只需要将_xml_routes直接放在routing_dev.yml下,否则它会以某种方式在loop.explanations中结束为什么是赞赏。