Need Help Integrating Twig View and Slim 4 using containers

So I’m using a skeleton I found online…

The “boostrap.php” looks like

use DI\ContainerBuilder;
use Slim\App;

require_once __DIR__ . '/../vendor/autoload.php';

$containerBuilder = new ContainerBuilder();

// Add DI container definitions
$containerBuilder->addDefinitions(__DIR__ . '/container.php');

// Create DI container instance
$container = $containerBuilder->build();

// Create Slim App instance
$app = $container->get(App::class);

// Register routes
(require __DIR__ . '/routes.php')($app);

// Register middleware
(require __DIR__ . '/middleware.php')($app);

return $app;

In the container I’m adding TWIG I think correctly…

return [
    'settings' => function () {
        return require __DIR__ . '/settings.php';

    App::class => function (ContainerInterface $container) {

        return AppFactory::create();

    ErrorMiddleware::class => function (ContainerInterface $container) {
        $app = $container->get(App::class);
        $settings = $container->get('settings')['error'];

        return new ErrorMiddleware(

    Twig:class => function (ContainerInterface $container){
        return new Twig(__DIR__.'/../views',[
            'cache' => __DIR__ . '/../cache',
            'auto_reload' => true
     TwigMiddleware::class => function (ContainerInterface $container){
        return TwigMiddleware::createFromContainer($container->get(App::class));

 etc etc etc

So I’m thinking this is correct however I also need to setup the middleware for the Twig and also be able to use it in the Classes/Controller/Routes…

In my Middleware


use Selective\BasePath\BasePathMiddleware;
use Slim\App;
use Slim\Middleware\ErrorMiddleware;
use Slim\Views\TwigMiddleware;
use Slim\Views\Twig;

return function (App $app) {
    // Parse json, form data and xml

    // Add the Slim built-in routing middleware


    // Handle exceptions

The Current error is

Message: The specified container key does not exist: view
File: /home/webuser/testsite/genapp/vendor/slim/twig-view/src/TwigMiddleware.php

So I’m not sure how to set the key I guess of for Twig instance/class.

I’m new to this format of DI so I’m unsure how to add the middleware and then use it throughout the app…


Are you sure your container’s definitions contain an entry:

return [
    'view' => function () {

or maybe you just confused view with Twig::class key within your middleware call from the container?

The TwigMiddleware uses the key “twig” as default. To make it work, change the DI container definition as follows:

use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
// ...

TwigMiddleware::class => function (ContainerInterface $container) {
    return TwigMiddleware::createFromContainer(
1 Like

My final solution which appeared to work is in the bootstrap

‘view’ => function (ContainerInterface $container){
return Twig::create(DIR.’/…/views’,[
‘cache’ => DIR . ‘/…/cache’,
‘auto_reload’ => true
TwigMiddleware::class => function (ContainerInterface $container){
return TwigMiddleware::createFromContainer($container->get(App::class));

in middleware.php