问题描述
我需要一种方法来覆盖树枝中的基本path()函数.
I need a way to override the basic path() function in twig.
我已经有一个小树枝扩展名,但是在那里定义路径过滤器对我没有帮助.
I have a twig extension already, but defining a path filter there did not do the trick for me.
谢谢.
推荐答案
扩展默认的路由扩展类.
Extend the default routing extension class.
use Symfony\Bridge\Twig\Extension\RoutingExtension;
class MyRoutingExtension extends RoutingExtension
{
public function getPath($name, $parameters = array(), $relative = false)
{
//some code
}
}
然后使用参数指定班级:
Then specify your class using a parameter:
parameters:
twig.extension.routing.class: MyNamespace\MyRoutingExtension
================================================ ======================
======================================================================
要注入更多的依赖项(例如域),您基本上需要复制服务定义,然后添加您的内容:
To inject more dependencies such as the domain you basically need to copy the service definition and then add your stuff:
# services.yml
twig.extension.routing:
class: '%twig.extension.routing.class%'
public: false
arguments:
- '@router'
- 'domain'
class MyRoutingExtension extends RoutingExtension
{
protected $domain;
public function __construct($router,$domain)
{
parent::__construct($router);
$this->domain = $domain;
}
}
您还可以从DependencyInjection扩展内部将参数添加到服务定义中.与复制服务定义相比,这可能会更健壮.如果更新symfony,则始终存在定义可能更改的风险.不太可能.我碰巧没有一个方便的例子.
You could also add the argument to the service definition from inside of your DependencyInjection extension. That might be a bit more robust that copying the service definition. There is always the risk that the definition might change if symfony is updated. Unlikely. I don't happen to have an example handy.
顺便说一句,您应该避免将一个问题添加到现有问题中.一些沮丧的选民会持暗淡的看法.
By the way, you should probably avoid adding a question to an existing question. Some of the down voters will take a dim view.
这篇关于Symfony2 Twig覆盖默认路径功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!