1

What is the file/class that ultimately executes the Twig url() function, like here: <a href="{{ url('app_home') }}">Home</a> in twig.

I know I can use a decorator to change functions with something like:

App\Services\MyRouter:
    decorates: 'router'
    arguments: ['@App\Services\MyRouter.inner']

and

public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
{
    $name = 'test'.$name;

    return $this->router->generate($name, $parameters, $referenceType);
}

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.

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);
    }

}
Bradmage
  • 1,233
  • 1
  • 15
  • 41
  • What **exactly** do you want to achieve? You want to change the generation of routes, but only for the Twig part? Only for some routes? Only in very specific cases? – Nico Haase Apr 08 '21 at 09:16
  • I mentioned that all I want to change is the actual output of `{{ url() }}`, I'm not after how to make the changes, just the the file I need to decorate. – Bradmage Apr 08 '21 at 09:56
  • Why not use another Twig function then? What do you want to achieve? – Nico Haase Apr 08 '21 at 09:59
  • My other [question](https://stackoverflow.com/q/66949490/1246494 ) might help. I just need to modify the output of the url() function. – Bradmage Apr 08 '21 at 10:23
  • To that new error: you forgot to inject the decorated service into your extension. Why not inject `UrlGeneratorInterface` instead? – Nico Haase Apr 09 '21 at 05:40
  • @NicoHaase I tried adding `__construct(UrlGeneratorInterface $generator)` and changing my method back to `return $this->generator->generate(...)` with the same result. – Bradmage Apr 09 '21 at 06:22
  • What is "the same result"? Please open a new question or edit the current one to contain all relevant information – Nico Haase Apr 09 '21 at 06:36
  • Sorry I meant to say same exception, so no change in outcome. I have edited my question with my changes. But I've made a new question now I've learnt a bit more about services, https://stackoverflow.com/questions/67016566/how-to-use-symfony-decorator-pattern-to-change-a-twig-function – Bradmage Apr 09 '21 at 07:11

1 Answers1

2

The url twig function is executed by Symfony\Bridge\Twig\Extension\RoutingExtension::getUrl().

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 twig.extension.routing.

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.

yivi
  • 42,438
  • 18
  • 116
  • 138
  • Your resources look like what I'm after to answer my question. Creating my own twig function was my first thought, until I discovered decorating. Do you have any good references for twig functions? – Bradmage Apr 08 '21 at 09:32
  • You can use that one as an example. Documentation is [here](https://symfony.com/doc/current/templating/twig_extension.html). – yivi Apr 08 '21 at 09:34
  • I'll look into that. But thinking about it, decorating the function might be a better solution for front end designers, they can keep using the standards they know. – Bradmage Apr 08 '21 at 10:25
  • 1
    Whatever approach you find better. The answer should help you either way. – yivi Apr 08 '21 at 10:26
  • I've been working with `twig.extension.routing` but I must be doing something wrong. I've updated my answer also. – Bradmage Apr 09 '21 at 01:52