[英]Symfony2 Twig overriding default path function
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. 我已经有一个twig扩展,但是定义一个路径过滤器并没有为我做这个技巧。
Thanks in advance. 提前致谢。
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;
}
}
You could also add the argument to the service definition from inside of your DependencyInjection extension. 您还可以从DependencyInjection扩展内部向服务定义添加参数。 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.
如果更新symfony,则定义可能会发生变化。 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.
一些失败的选民将采取一种暗淡的观点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.