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

标签 symfony bundle router

我准备了外部包,我想在编译过程中添加一些路由。 路由将在主 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/

相关文章:

java - Spring Integration Java DSL - 动态设置 RecepientListRouter 收件人?

datetime - twig 文件中的 Symfony 日期到 Hijri 日期转换

symfony - 在 JMS 中序列化对象时包含方法

Symfony2 : Using the Finder Component for S3

symfony - 在实体类中访问 Symfony2 全局参数

ruby - 在 Gemfile 中设置环境以根据自定义文件 bundle 安装/更新

angular - 如何在 Angular 5 中嵌套超过 2 级的子路由?

php - 当 bundle 产品位于购物车中时,从 Woocommerce 中的购物车中删除子组件

macos - 无法在 OS X 10.10 上使用 Qt 框架签署应用程序包

java - Akka(java),对所有 child 的非阻塞广播