更改Symfony中的Twig url()输出 [英] Change Twig url() output in Symfony
问题描述
最终执行Twig url()
函数的文件/类是什么,如下所示:< a href =&; {{url('app_home')}}''';> Home</a>
.
我知道我可以使用装饰器来更改功能,例如:
App \ Services \ MyRouter:装饰:路由器"参数:['@App \ Services \ MyRouter.inner']
和
公共函数generate($ name,$ parameters = [],$ referenceType = self :: ABSOLUTE_PATH){$ name ='test'.$ name;返回$ this->路由器-> generate($ name,$ parameters,$ referenceType);}
以上内容更改了影响其他区域(例如加载控制器)的路线.
我所需要的只是树枝文档中的最终输出.我无法找到正确的装饰服务.
修改
根据答案,我一直在使用twig.extension.routing,但现在我只得到了未知"路径.功能."例外.我的期望是什么都不会发生,并且我的函数返回了原始方法.
App \ Service \ TwigUrlDecorator:装饰:"twig.extension.routing"参数:['@App \ Service \ TwigUrlDecorator.inner']公开:错误<?php//src/Service/TwigUrlDecorator.php命名空间App \ Service;使用Twig \ Extension \ AbstractExtension;TwigUrlDecorator类扩展AbstractExtension{公共函数getPath($ name,$ parameters = array(),$ relative = false){返回parent :: getPath($ name,$ parameters,$ relative);}}
url
twig函数由 Symfony \ Bridge \ Twig \ Extension \ RoutingExtension :: getUrl()执行代码>.
您可以找到类定义此处.该服务在此处定义,您可以在其中看到服务名称的是 twig.extension.routing
.
我想您可以修饰扩展名,但是考虑到它的简单性,只需 and The above changes the route which affects other areas like loading controllers. All I'm after is the final output in the twig document. I haven't been able to find the the correct service to decorate. edit Based off the answers I have been working with twig.extension.routing but now I just get an "Unknown "path" function." exception. My expectation would be for nothing to happen and my function returns the original method.
The You can find the class definition here, and the specific method here. The service is defined here, where you can see the service name is I guess you could decorate the extension, but considering how simple it is, it might be simpler just to define your own URL generating twig function by defining a new Twig Extension. 这篇关于更改Symfony中的Twig url()输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!App\Services\MyRouter:
decorates: 'router'
arguments: ['@App\Services\MyRouter.inner']
public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
{
$name = 'test'.$name;
return $this->router->generate($name, $parameters, $referenceType);
}
App\Service\TwigUrlDecorator:
decorates: 'twig.extension.routing'
arguments: ['@App\Service\TwigUrlDecorator.inner']
public: false
<?php
// src/Service/TwigUrlDecorator.php
namespace App\Service;
use Twig\Extension\AbstractExtension;
class TwigUrlDecorator extends AbstractExtension
{
public function getPath($name, $parameters = array(), $relative = false)
{
return parent::getPath($name, $parameters, $relative);
}
}
url
twig function is executed by Symfony\Bridge\Twig\Extension\RoutingExtension::getUrl()
.twig.extension.routing
.