对于尝试执行此操作的其他任何人,当您不熟悉 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] 删除。
我来说两句