Symfony 4.2 - How to decorate the UrlGenerator

后端 未结 3 1334
失恋的感觉
失恋的感觉 2021-01-05 14:52

I want to decorate the Symfony UrlGenerator class.

Symfony\\Component\\Routing\\Generator\\UrlGenerator: ~

my.url_generator:
    class: AppBund         


        
3条回答
  •  一生所求
    2021-01-05 15:05

    The right answer is : you shouldn't decorate UrlGeneratorInterface. You have to decorate 'router' service. Check here : https://github.com/symfony/symfony/issues/28663

    ** services.yml :

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

    ** MyRouter.php :

    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);
        }
    }
    

提交回复
热议问题