Symfony - 动态更改配置值

用户2559108

我正在使用 Symfony CMF,它有一个 RoutingAutoBundle,它公开了这些参数:

cmf_routing_auto:
    ....
    persistence:
        phpcr:
            route_basepath: /routes

我需要在旅途中动态设置 route_basepath,是否可以动态更改 config.yml 值?

我正在构建一个对每个用户都隔离的 CMS,为此,我将每个用户的路由存储在他自己的 PHPCR 文档中。

我的想法是在早期的请求侦听器中根据请求 HTTP_HOST 和/或子域更改路由器基本路径。

编辑

这是结构。

在此处输入图片说明

用户2559108

对于尝试执行此操作的其他任何人,当您不熟悉 CMF 包和 AutoRouting 时,这是一项相当复杂的任务,route_basepath 无法由事件侦听器等动态更改,如果您依赖于请求,则请求将不可用在那时候。

我设法通过执行以下操作来做到这一点:

覆盖“cmf_routing.phpcr_candidates_prefix”服务,并将对其的所有引用替换为您自己的候选前缀服务。

class OverrideRoutePrefixListener implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $routePrefixDefinition = $container->getDefinition('cmf_routing.phpcrodm_route_idprefix_listener');
        $routePrefixDefinition->replaceArgument(0, new Reference('app.phpcr_candidates_prefix'));

        $routeLocalePrefixDefinition = $container->getDefinition('cmf_routing.phpcrodm_route_locale_listener');
        $routeLocalePrefixDefinition->replaceArgument(0, new Reference('app.phpcr_candidates_prefix'));
    }
}

您自己的服务应该继承原始服务并更改构造函数,您可能需要复制一堆私有字段等。

public function __construct(array $prefixes, array $locales = array(), ManagerRegistry $doctrine = null, $limit = 20)
{
    // Do something else here, if you just want multiple prefixes, its supported by the config 
    // But if you want to fetch them from the database or the like, you have to do this.
    $prefixes = ['/users/admin/sites/test/routes', '/users/admin/sites/test/simple'];

    parent::__construct($prefixes, $locales = array(), $doctrine = null, $limit = 20);
    $this->setPrefixes($prefixes);

    $this->doctrine = $doctrine;
}

在您自己的 cmf_routing.phpcr_candidates_prefix 中,您可以自由地将前缀解析为您想要的任何内容。

第二步是覆盖 PhpcrOdmAdapter,您也可以使用编译器传递来完成此操作。

class OverridePhpcrOdmAdapter implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $definition = $container->getDefinition('cmf_routing_auto.adapter.phpcr_odm');
        $definition->setClass(PhpcrOdmAdapter::class);
    }
}

并在构造函数处更改基本路径。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章