Auto registration Twig extensions with arguments
opened this issue · comments
Deleted user commented
How to do this?
Victor Kalmuk commented
You can implement ContainerAwareInterface, from container get services, parameters. For example Router service.
<?php
namespace App\Twig;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Router;
/**
* Twig main extension
*/
class AmountFieldExtension extends \Twig_Extension implements ContainerAwareInterface
{
/** @var ContainerInterface $container */
private $container;
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
/**
* @return Router
*/
protected function getRouter()
{
return $this->container->get('router');
}
Florian Klein commented
yes, @vkalmuk example is the only possibility.
As soon as you ned arguments, you need to create the service yourself.