Symfony2 Twig overrides the default path function

I need a way to override the main path () function in a branch.

I already have a branch extension, but defining a path filter there did not help.

Thanks in advance.

+4
source share
1 answer

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 the parameter:

parameters:
    twig.extension.routing.class: MyNamespace\MyRoutingExtension

==================================================== ======================

To add more dependencies, such as a domain in which 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 . . .

, , , . .

+11

Source: https://habr.com/ru/post/1542667/


All Articles