Symfony 4:我装饰了URLGenerator界面,但它';未使用,而是使用CompiledUrlGenerator

Symfony 4:我装饰了URLGenerator界面,但它';未使用,而是使用CompiledUrlGenerator,symfony,routing,decorator,symfony4,router,Symfony,Routing,Decorator,Symfony4,Router,我打开了一个新的GeneratorInterface app.decorator.url_generator: class: App\CoreBundle\Routing\Extension\UrlGenerator decorates: Symfony\Component\Routing\Generator\UrlGeneratorInterface arguments: ['@app.decorator.url_generator.inner'] 但在示例中某些bu

我打开了一个新的GeneratorInterface

app.decorator.url_generator:
    class: App\CoreBundle\Routing\Extension\UrlGenerator
    decorates: Symfony\Component\Routing\Generator\UrlGeneratorInterface
    arguments: ['@app.decorator.url_generator.inner']
但在示例中某些bundle执行$this->generator->generate()的情况下不使用它,我通过XDebug跟踪了Symfony所做的工作,并使用CompiledUrlGenerator。我可以看到发生这种情况的地方,即在getGenerator中的Symfony\Component\Routing\Router中,它专门检查CompiledUrlGenerator::class。但我不想重写普通的Symfony代码。我应该如何覆盖/修饰/扩展哪个类,以便始终选择我的类,因为我需要向路径添加特殊参数。提前谢谢你

我找到了

app.decorator.router:
    class: App\CoreBundle\Routing\Extension\Router
    decorates: 'router.default'
    arguments: ['@app.decorator.router.inner']
装饰这实际上使所有包都使用你的路由器。作为UrlGenerator,它具有可扩展的generate函数

编辑:根据请求,我还提供路由器类:

class Router implements RouterInterface {
    protected $innerRouter;
    public function __construct(RouterInterface $innerRouter) {
        $this->innerRouter = $innerRouter;
    }
    public function setContext(RequestContext $context)
    {
        $this->innerRouter->setContext($context);
    }
    public function getContext()
    {
        return $this->innerRouter->getContext();
    }
    public function getRouteCollection()
    {
        return $this->innerRouter->getRouteCollection();
    }
    public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
    {
        //add here to $parameters...
        return $this->innerRouter->generate($name, $parameters, $referenceType);
    }
    public function match($pathinfo)
    {
        $parameters = $this->innerRouter->match($pathinfo);
        //add here to $parameters...
        return $parameters;
    }
}
我找到了

app.decorator.router:
    class: App\CoreBundle\Routing\Extension\Router
    decorates: 'router.default'
    arguments: ['@app.decorator.router.inner']
装饰这实际上使所有包都使用你的路由器。作为UrlGenerator,它具有可扩展的generate函数

编辑:根据请求,我还提供路由器类:

class Router implements RouterInterface {
    protected $innerRouter;
    public function __construct(RouterInterface $innerRouter) {
        $this->innerRouter = $innerRouter;
    }
    public function setContext(RequestContext $context)
    {
        $this->innerRouter->setContext($context);
    }
    public function getContext()
    {
        return $this->innerRouter->getContext();
    }
    public function getRouteCollection()
    {
        return $this->innerRouter->getRouteCollection();
    }
    public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
    {
        //add here to $parameters...
        return $this->innerRouter->generate($name, $parameters, $referenceType);
    }
    public function match($pathinfo)
    {
        $parameters = $this->innerRouter->match($pathinfo);
        //add here to $parameters...
        return $parameters;
    }
}

你有路由器类的例子吗?我试图做一些类似的事情,但找不到方法。我在答案中添加了路由器类的实现。希望有帮助!非常感谢。它很有魅力,我很难弄明白。是的,这里也是。你有路由器课程的例子吗?我试图做一些类似的事情,但找不到方法。我在答案中添加了路由器类的实现。希望有帮助!非常感谢。它就像一个符咒,我很难弄明白。是的,这里也是。