[Slim3] Twig as a service provder?

Hello all,

I’ve been experimenting with Slim 3 & 4 and recently with Slim + Doctrine. I was led to the Full Example via the userguide and was able to create some additional entities and return the results via JSON.

I should note that I am using Commit dfa2ef3 (Slim3 version)

Now I am attempting to follow the ‘Provider’ example and use it for Twig (slim\twig-view) but I keep getting errors; the latest of which is Argument 1 passed to Slim\Views\Twig::createLoader() must be of the type array, null given.

Here is my attempt; I will warn I am a hobbyist/novice and teaching myself PHP, so be gentle :slight_smile:

src/Provider/Twig.php

<?php

declare(strict_types=1);

namespace App\Provider;

use Pimple\{Container, ServiceProviderInterface};
use Slim\{Http\Environment, Http\Uri, Views\TwigExtension};

class Twig implements ServiceProviderInterface
{
    public function register(Container $c)
    {
        $c[\Slim\Views\Twig::class] = function ($c) {
            $view = new \Slim\Views\Twig(['settings']['view']['template_path'], ['settings']['view']['twig']);

            // Instantiate and add Slim specific extension
            $router = $c('router');
            $uri = Uri::createFromEnvironment(new Environment($_SERVER));
            $view->addExtension(new TwigExtension($router, $uri));

            return $view;
        };
    }
}

src/Action/HomeAction.php

<?php

declare(strict_types=1);

namespace App\Action;

use Slim\{Http, Views\Twig};

class HomeAction
{
    private $view;

    public function __construct(Twig $view)
    {
        $this->view = $view;
    }

    public function __invoke(Http\Request $request, Http\Response $response, $args)
    {
        $result = $this->view->render($response,'index.twig', $data);

        return $response;
    }
}

And added the following to src/Provider/Slim.php

$c[Home::class] = function (Container $c): Home {
        return new Home($c[\Slim\Views\Twig::class]);
    };

Any help is appreciated. Thanks!