I'm trying to change the output of {{ url('app_route_name') }} or {{ path('app_route_name') }}
. Normally the output might be /app/route/name
I'm wanting to modify the twig output only.
My first attempt was decorating UrlGenerator->generate, but changing $name also changed the controller called. I just want to change the final output.
Here is the code I'm currently trying. The error exception I'm getting is:
Unknown "path" function. Did you mean "logout_path", "relative_path", "impersonation_exit_path"?
App\Service\TwigUrlDecorator:
decorates: 'twig.extension.routing'
arguments: ['@.inner']
public: false
// src/Service/TwigUrlDecorator.php
namespace App\Service;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Extension\AbstractExtension;
use Symfony\Bridge\Twig\Extension\RoutingExtension;
class TwigUrlDecorator extends AbstractExtension
{
private $generator;
private $router;
public function __construct(RoutingExtension $router, UrlGeneratorInterface $generator)
{
$this->router = $router;
$this->generator = $generator;
}
public function getPath(string $name, array $parameters = [], bool $relative = false): string
{
return $this->generator->generate($name, $parameters, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH);
}
public function getUrl(string $name, array $parameters = [], bool $schemeRelative = false): string
{
return $this->generator->generate($name, $parameters, $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
}
}
My original question might shed some background on what I'm looking for.