title: Render Twig templates in Controller - Slim 4 Framework
body: <p>Like said in the title, I looking to rendering twig template in controller using Slim 4 framework.</p>
  
  <p>I searched on the Internet, but I didn't find a solution that works for me and well-explained.</p>
  
  <p>If someone can explains me how can I make this to work.</p>
  
  <p>Here the code a my files:</p>
  
  <p>index.php</p>
  
  <pre><code>&lt;?php
  use App\Controllers\HomeController;
  use DI\Container;
  use Slim\Factory\AppFactory;
  
  require __DIR__ . '/../vendor/autoload.php';
  
  ### Container ###
  $container = new Container();
  
  ### Slim ###
  AppFactory::setContainer($container);
  $app = AppFactory::create();
  
  ### Twig ###
  $container = $app-&gt;getContainer();
  $container['view'] = function ($c) {
      return $c;
  };
  $container['HomeController'] = function ($c) {
      return new HomeController($c['view']);
  };
  
  ### Routes ###
  $app-&gt;get('/',HomeController::class . ":home");
  $app-&gt;get('/home', HomeController::class . ":home");
  
  ### Run ###
  $app-&gt;run();
  </code></pre>
  
  <p>HomeController.php</p>
  
  <pre><code>&lt;?php
  
  namespace App\Controllers;
  
  use Psr\Container\ContainerInterface;
  use Slim\Psr7\Request;
  use Slim\Psr7\Response;
  
  class HomeController
  {
      private $app;
  
      public function __construct(ContainerInterface $app)
      {
          $this-&gt;app = $app;
      }
  
      public function home(Request $request, Response $response)
      {
          $this-&gt;app-&gt;get('view')-&gt;render($response, 'home.twig');
      }
  }
  </code></pre>
  
  <p>composer.json</p>
  
  <pre><code>{
      "require": {
          "slim/slim": "4.*",
          "slim/psr7": "^1.0",
          "slim/twig-view": "^3.0",
          "php-di/php-di": "^6.0"
      },
      "autoload": {
          "psr-4": {
              "App\\": "app/"
          }
      }
  }
  
  </code></pre>
  
  <p>project structure</p>
  
  <p><a href="https://i.stack.imgur.com/Ak2db.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ak2db.png" alt="enter image description here"></a></p>
  
  <p>And when I start the server, the console gives me:</p>
  
  <pre><code>PHP 7.3.11-0ubuntu0.19.10.2 Development Server started at Tue Feb 18 16:33:41 2020
  Listening on http://localhost:8080
  Document root is /home/thomas/Code/2020/S4/prog_web/homelogin/public
  Press Ctrl-C to quit.
  [Tue Feb 18 16:33:44 2020] PHP Fatal error:  Uncaught Error: Cannot use object of type DI\Container as array in /home/thomas/Code/2020/S4/prog_web/homelogin/public/index.php:17
  Stack trace:
  #0 {main}
    thrown in /home/thomas/Code/2020/S4/prog_web/homelogin/public/index.php on line 17
  
  </code></pre>
  
  <p>Thank in advance.</p>