How to Dependency Injection in drupal ?¶
we are going to explain how dependency injection with twig as example .
Resources¶
Dependency Injection¶
In Dependency Injection we have over three solutions to load and render TWIG as example
1. By implement ContainerInjectionInterface
interface in your controller¶
implement this interface you must add create
method like this
class CustomController implements ContainerInjectionInterface {
protected $twig ;
public function __construct(\Twig_Environment $twig)
{
$this->twig = $twig ;
}
public static function create(ContainerInterface $container)
{
return new static(
$container->get('twig')
);
}
}
``public static function create(ContainerInterface $container)``
make sure your function is public and static because it called without instance of your controller
return new static();
this new concept called (late static binding LSP) start with php 5.3+ you will find info here
more info self vs static
$container->get('twig')
this load twig container and send to constructor as parameter .
protected $twig ;
declare a variable , you can avoid this step and use magic function as you like .
public function __construct(\Twig_Environment $twig)
in constructor must receive twig as parameter and assign to protected twig variable .
NOTES:
- The responsible class about call your create function is ClassResolver and method is getInstanceFromDefinition
2. By implement ContainerAwareInterface interface in your controller¶
- After implement this interface you must add (setContainer) method like this .
class CustomController implements ContainerAwareInterface {
protected $twig ;
public function setContainer(ContainerInterface $container = null)
{
$this->twig = $container->get('twig') ;
}
}
$this->twig = $container->get('twig') ;
to assign twig container to
twig variable
NOTES:
- make sure you are implement (ContainerAwareInterface)[#]
- The responsible class about call your create function is ClassResolver and method is getInstanceFromDefinition
3. By get container directly ( not recommended )¶
$twig = \Drupal::getContainer()->get('twig') ;
Common Questions¶
- You should make sure that your logging and errors is enable ‘’ yoursite/admin/config/development/logging ‘’ .