Search code examples
phpslim-3php-di

How to get the Twig object out of the container?


I'm using Slim Framework 3 with PHP-DI/Slim-Bridge. This is my container:

$builder->addDefinitions([
            'settings.displayErrorDetails' => true,

            'router' => get(Router::class),

            Twig::class => function(ContainerInterface $c) {
                $twig = new Twig(__DIR__.'/../resources/views', [
                   'cache'=> false
                ]);

                $twig->addExtension(new TwigExtension(
                    $c->get('router'),
                    $c->get('request')->getUri()
                ));

                return $twig;
            }
   ]);

How can I get the twig object back out of the container? I tried $container->twig, but I'm not able to get the object back.


Solution

  • Try $container->get(Twig::class).

    I invite you to read Getting started with PHP-DI.