我准备外部包,我想在编译过程中添加一些路由。
路线将在主app/config/config.yml
设置上创建。
我试图通过router
ContainerBuilder
从CustomCompilerPass
获取$definition = $container->getDefinition('router');
:
The service definition "router" does not exist
,但我得到{{1}}。
是否可以在编译过程中添加自定义路由?
答案 0 :(得分:3)
无法在编译器传递中添加路由 为了动态加载路径(了解容器参数),我使用custom route loader
中给出的previous exampleclass 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;
}
}
的的routing.yml 强>
_custom_routes:
resource: .
type: custom
答案 1 :(得分:0)
router
是别名,而不是服务。要从ContainerBuilder
获取,请使用ContainerBuilder::getAlias
。要获取服务ID,您需要将该对象强制转换为字符串:(string) $container->getAlias('router')
。现在,您可以使用该ID获取服务:$container->getDefinition($container->getAlias('router'))
。然后,您将获得可以修改的服务以添加路由。