我准备了外部包,我想在编译过程中添加一些路由。
路由将在主 app/config/config.yml
设置上创建。
我试图通过以下方式从我的 CustomCompilerPass
中的 ContainerBuilder
获取 router
:
$definition = $container->getDefinition('router');
,但我得到服务定义“路由器”不存在
。
是否可以在编译过程中添加自定义路由?
最佳答案
无法在编译器阶段添加路由。
为了动态加载路由(了解容器参数),我会使用 custom route loader正如我的 previous example 中给出的
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;
}
}
路由.yml
_custom_routes:
resource: .
type: custom
关于symfony - 是否可以在编译过程中添加自定义路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18617972/