如何使用Symfony Decorator模式更改Twig功能? [英] How to use Symfony Decorator pattern to change a Twig function?
问题描述
我正在尝试更改 {{url('app_route_name')}}或{{path('app_route_name')}}
的输出.通常,输出可能是/app/route/name
我只想修改树枝输出.
I'm trying to change the output of {{ url('app_route_name') }} or {{ path('app_route_name') }}
. Normally the output might be /app/route/name
I'm wanting to modify the twig output only.
我的第一次尝试是修饰UrlGenerator-> generate,但是更改$ name也会更改所调用的控制器.我只想更改最终输出.
My first attempt was decorating UrlGenerator->generate, but changing $name also changed the controller called. I just want to change the final output.
这是我当前正在尝试的代码.我收到的错误异常是:
Here is the code I'm currently trying. The error exception I'm getting is:
未知的路径";功能.您是说"logout_path","relative_path","impersonation_exit_path"吗?
Unknown "path" function. Did you mean "logout_path", "relative_path", "impersonation_exit_path"?
App\Service\TwigUrlDecorator:
decorates: 'twig.extension.routing'
arguments: ['@.inner']
public: false
// src/Service/TwigUrlDecorator.php
namespace App\Service;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Extension\AbstractExtension;
use Symfony\Bridge\Twig\Extension\RoutingExtension;
class TwigUrlDecorator extends AbstractExtension
{
private $generator;
private $router;
public function __construct(RoutingExtension $router, UrlGeneratorInterface $generator)
{
$this->router = $router;
$this->generator = $generator;
}
public function getPath(string $name, array $parameters = [], bool $relative = false): string
{
return $this->generator->generate($name, $parameters, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH);
}
public function getUrl(string $name, array $parameters = [], bool $schemeRelative = false): string
{
return $this->generator->generate($name, $parameters, $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
}
}
我最初的问题可能会为我所寻找的内容提供一些背景知识
My original question might shed some background on what I'm looking for.
推荐答案
您在哪里定义了Twig函数 path
?您已经修饰了 twig.extension.routing
而不定义任何Twig函数.当前,调用了AbstractExtension的方法 getFunctions
,这将返回一个空的函数列表.
Where did you define the Twig function path
? You've decorated twig.extension.routing
without defining any Twig functions. Currently, the method getFunctions
of the AbstractExtension is called, and this returns an empty list of functions.
与装饰中的常规方法一样:如果要在原始服务上执行任何方法,则需要路由此类调用.这样的事情可能会有所帮助:
As usual in decoration: if you want to execute any methods on the original service, you need to route such calls through. Something like this might help:
public function getFunctions(): array {
return $this->router->getFunction();
}
此外,您应该检查属性的命名.在当前状态下, $ router
不包含任何路由器,但包含用于路由的Twig扩展名.在我看来,这很令人困惑,因为适当的路由器将提供与Twig扩展完全不同的方法
Additionally, you should check the naming of the properties. In the current state, $router
does not contain any router, but the Twig extension for routing. To me, this looks pretty confusing, as a proper router would provide completely different methods than a Twig extension does
这篇关于如何使用Symfony Decorator模式更改Twig功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!