Symfony 4:我装饰了 UrlGeneratorInterface,但没有使用,它使用 CompiledUrlGenerator 代替
Symfony 4: I decorated UrlGeneratorInterface, but it's not used, it uses CompiledUrlGenerator instead
我修饰了 UrlGeneratorInterface
app.decorator.url_generator:
class: App\CoreBundle\Routing\Extension\UrlGenerator
decorates: Symfony\Component\Routing\Generator\UrlGeneratorInterface
arguments: ['@app.decorator.url_generator.inner']
但在示例中的某些包执行 $this->generator->generate() 的情况下,它不会被使用,我跟踪了 Symfony 通过 XDebug 做了什么,而是使用了 CompiledUrlGenerator。我可以看到发生这种情况的地方,即在 getGenerator 的 Symfony\Component\Routing\Router 中,它专门检查 CompiledUrlGenerator::class。但我不想覆盖香草 Symfony 代码。我应该如何 override/decorate/extend 哪个 class 才能始终选择我的,因为我有特殊参数需要添加到路径中。提前致谢!
我找到了。
app.decorator.router:
class: App\CoreBundle\Routing\Extension\Router
decorates: 'router.default'
arguments: ['@app.decorator.router.inner']
装饰它实际上使所有包都使用您的路由器。作为UrlGenerator,它具有可以扩展的生成功能。
编辑:应要求我也提供路由器 class:
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;
}
}
我修饰了 UrlGeneratorInterface
app.decorator.url_generator:
class: App\CoreBundle\Routing\Extension\UrlGenerator
decorates: Symfony\Component\Routing\Generator\UrlGeneratorInterface
arguments: ['@app.decorator.url_generator.inner']
但在示例中的某些包执行 $this->generator->generate() 的情况下,它不会被使用,我跟踪了 Symfony 通过 XDebug 做了什么,而是使用了 CompiledUrlGenerator。我可以看到发生这种情况的地方,即在 getGenerator 的 Symfony\Component\Routing\Router 中,它专门检查 CompiledUrlGenerator::class。但我不想覆盖香草 Symfony 代码。我应该如何 override/decorate/extend 哪个 class 才能始终选择我的,因为我有特殊参数需要添加到路径中。提前致谢!
我找到了。
app.decorator.router:
class: App\CoreBundle\Routing\Extension\Router
decorates: 'router.default'
arguments: ['@app.decorator.router.inner']
装饰它实际上使所有包都使用您的路由器。作为UrlGenerator,它具有可以扩展的生成功能。
编辑:应要求我也提供路由器 class:
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;
}
}