简体   繁体   English

Symfony 4.2 - 如何装饰 UrlGenerator

[英]Symfony 4.2 - How to decorate the UrlGenerator

I want to decorate the Symfony UrlGenerator class.我想装饰 Symfony UrlGenerator类。

Symfony\Component\Routing\Generator\UrlGenerator: ~

my.url_generator:
    class: AppBundle\Service\UrlGenerator
    decorates: Symfony\Component\Routing\Generator\UrlGenerator
    arguments: ['@my.url_generator.inner']
    public:    false

I've added this to the services.yml but my AppBundle\\Service\\UrlGenerator class is ignored:我已将此添加到services.yml但我的AppBundle\\Service\\UrlGenerator类被忽略:

I tried the following configuration again.我再次尝试了以下配置。

config/services.yaml

parameters:
    locale: 'en'
    router.options.generator_class: AppBundle\Service\UrlGenerator
    router.options.generator_base_class: AppBundle\Service\UrlGenerator

Still it doesn't work还是不行

How to decorate the UrlGenerator in Symfony 4.2?如何在 Symfony 4.2 中装饰UrlGenerator

The right answer is : you shouldn't decorate UrlGeneratorInterface.正确答案是:你不应该装饰 UrlGeneratorInterface。 You have to decorate 'router' service.您必须装饰“路由器”服务。 Check here : https://github.com/symfony/symfony/issues/28663在这里查看: https : //github.com/symfony/symfony/issues/28663

** services.yml : ** 服务.yml :

services:
    App\Services\MyRouter:
        decorates: 'router'
        arguments: ['@App\Services\MyRouter.inner']

** MyRouter.php : ** MyRouter.php :

<?php

namespace App\Services;

use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouterInterface;

class MyRouter implements RouterInterface
{
    /**
     * @var RouterInterface
     */
    private $router;

    /**
     * MyRouter constructor.
     * @param RouterInterface $router
     */
    public function __construct(RouterInterface $router)
    {
        $this->router = $router;
    }

    /**
     * @inheritdoc
     */
    public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
    {
        // Your code here

        return $this->router->generate($name, $parameters, $referenceType);
    }

    /**
     * @inheritdoc
     */
    public function setContext(RequestContext $context)
    {
        $this->router->setContext($context);
    }

    /**
     * @inheritdoc
     */
    public function getContext()
    {
        return $this->router->getContext();
    }

    /**
     * @inheritdoc
     */
    public function getRouteCollection()
    {
        return $this->router->getRouteCollection();
    }

    /**
     * @inheritdoc
     */
    public function match($pathinfo)
    {
        return $this->router->match($pathinfo);
    }
}

I believe the issue is that UrlGenerator service name is Symfony\\Component\\Routing\\Generator\\UrlGeneratorInterface , and not Symfony\\Component\\Routing\\Generator\\UrlGenerator (cf. this code ).我认为问题在于 UrlGenerator 服务名称是Symfony\\Component\\Routing\\Generator\\UrlGeneratorInterface ,而不是Symfony\\Component\\Routing\\Generator\\UrlGenerator (参见此代码)。

Secondly, when you decorate a service, the decorator will take the service name.其次,当你装饰一个服务时,装饰者会取服务名称。 So you should not need to modify router.options.generator_class .所以你不需要修改router.options.generator_class

Try with this configuration:试试这个配置:

my.url_generator:
    class: AppBundle\Service\UrlGenerator
    decorates: Symfony\Component\Routing\Generator\UrlGeneratorInterface
    arguments: ['@my.url_generator.inner']

Setting public to false is likely not needed, as on Symfony4/Flex it should be the default value.可能不需要将public设置为false ,因为在 Symfony4/Flex 上它应该是默认值。

Update for comments: decorated service may look like this:评论更新:装饰服务可能如下所示:

class MyUrlGenerator implements UrlGeneratorInterface
{
    private $originalUrlGenerator;

    public function __construct(UrlGeneratorInterface $innerUrlGenerator)
    {
        $this->originalUrlGenerator = $innerUrlGenerator;
    }

    public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
    {
        // Maybe add your custom logic here... 
        // or completely override base method

        return $this->originalUrlGenerator->generate($name, $parameters, $referenceType);
    }
}

我相信你必须装饰Symfony\\Component\\Routing\\Generator\\UrlGeneratorInterface ,因为服务应该依赖于接口而不是特定的实现(类)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM