是否可以在编译过程中添加自定义路由?

NHG*_*NHG 5 router bundle symfony

我准备外部包,我想在编译过程中添加一些路由.将在主要app/config/config.yml设置上创建路线.

我试图routerContainerBuilder我的CustomCompilerPass通道中获取:

$definition = $container->getDefinition('router');
Run Code Online (Sandbox Code Playgroud)

,但我得到了The service definition "router" does not exist.

是否可以在编译过程中添加自定义路由?

Tou*_*uki 3

无法在编译器传递中添加路由。
为了动态加载路由(了解容器参数),我将使用前面示例中给出的自定义路由加载器

class MyLoader extends Loader
{
    protected $params;

    public function __construct($params)
    {
        $this->params = $params;
    }

    public function supports($resource, $type = null)
    {
        return $type === 'custom' && $this->params == 'YourLogic';
    }

    public function load($resource, $type = null)
    {
        // This method will only be called if it suits the parameters
        $routes   = new RouteCollection;
        $resource = '@AcmeFooBundle/Resources/config/dynamic_routing.yml';
        $type     = 'yaml';

        $routes->addCollection($this->import($resource, $type));

        return $routes;
    }
}
Run Code Online (Sandbox Code Playgroud)

routing.yml

_custom_routes:
    resource: .
    type:     custom
Run Code Online (Sandbox Code Playgroud)