gpt4 book ai didi

php - Symfony 4.2 - 如何装饰 UrlGenerator

转载 作者:可可西里 更新时间:2023-10-31 23:26:56 24 4
gpt4 key购买 nike

我想装饰 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

我已将其添加到 services.yml 但我的 AppBundle\Service\UrlGenerator 类被忽略了:

我再次尝试了以下配置。

config/services.yaml

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

还是不行

如何在 Symfony 4.2 中修饰 UrlGenerator

最佳答案

正确答案是:你不应该装饰 UrlGeneratorInterface。你必须装饰“路由器”服务。在这里查看:https://github.com/symfony/symfony/issues/28663

** 服务.yml :

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

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

关于php - Symfony 4.2 - 如何装饰 UrlGenerator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55468435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com