# \[Slim3\] Twig as a service provder?

**URL:** <https://discourse.slimframework.com/t/slim3-twig-as-a-service-provder/3891>\
**Category:** Questions\
**Created:** [February 24, 2020, 5:54pm UTC](https://discourse.slimframework.com/t/slim3-twig-as-a-service-provder/3891 "2020-02-24T17:54:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jiinx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/jiinx/32/1085_2.png) [@Jiinx](https://discourse.slimframework.com/u/Jiinx)\
**Post date:** [February 24, 2020, 5:54pm UTC](https://discourse.slimframework.com/t/slim3-twig-as-a-service-provder/3891/1 "2020-02-24T17:54:40Z")

</div>

Hello all,

I’ve been experimenting with Slim 3 & 4 and recently with Slim + Doctrine. I was led to the [Full Example](https://github.com/1ma/Slim-Doctrine-Demo) 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](https://github.com/1ma/Slim-Doctrine-Demo/tree/dfa2ef3500aedc98b01588c02bce822cf19a3d3a) (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 🙂

_src/Provider/Twig.php_

```auto
<?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_

```auto
<?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_

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

```

Any help is appreciated. Thanks!
