Call to a member function render() on null - Twig + Slim framework 3

Hi guys, i using Slim framework 3 + Twig, i encountered the following problem:
Notice: Undefined property: TestController::$view in D:\xampp\htdocs\classes\testclass.php on line 13
Fatal error: Call to a member function render() on null in D:\xampp\htdocs\classes\testclass.php on line 13

Here is my code index.php
include ‘vendor/autoload.php’;
require ‘classes/testclass.php’;
// Create app
$app = new \Slim\App();
// Get container
$container = $app->getContainer();
// Register component on container
$container[‘view’] = function ($container) {
$view = new \Slim\Views\Twig(’./templates’, [
‘cache’ => ‘./cache’,
‘auto_reload’ => true
$view->addExtension(new \Slim\Views\TwigExtension(
return $view;
$app->get(’/testData/{name}’, ‘\TestController:method1’);
// Run app

My testclass.php

<?php use Interop\Container\ContainerInterface; class TestController { protected $ci; //Constructor public function __construct(ContainerInterface $ci) { $this->ci = $ci; } public function method1($request, $response, $args) { $this->view->render($response, '../templates/testCt.html.twig', [ 'agr'=> 'Hello World №1!' ]); } public function method2($request, $response, $args) { $this->view->render($response, '../templates/testCt.html.twig', [ 'agr'=> 'Hello World №2!' ]); } public function method3($request, $response, $args) { $this->view->render($response, '../templates/testCt.html.twig', [ 'agr'=> 'Hello World! №3' ]); } } ---------- I am a newbie and poorly understand how to work with Slim Framework and Twig, i'm just not good at php. Help me please to solve this problem.


It looks like you are trying to access the view $this->view-> but you haven’t passed it in nor grabbed it from the container yet. You will need to fetch the view from the container $this->ci->get('view') before using it.

The relevant section of the docs is Container Resolution.

If you need a little more of a primer, I’d have a look at the slim3-skeleton by the amazing @akrabat. Have a look at where the view is set in the container, where it is passed to the action/controller, and then the various places where it is used in the controller $view and $this->view.

Hello tflight, thank you very much for your response[quote=“tflight, post:2, topic:658”]
slim3-skeleton by the amazing @akrabat [/quote] is the perfect solution to my problem, I do not know why I have not used it since the beginning of the project.

But now I had a another problem, in the project I use Medoo framework to access to DB.
Before that I used such a structure:
$db = new medoo([
‘database_type’ => ‘mysql’,
‘database_name’ => ‘curs’,
‘server’ => ‘localhost’,
‘username’ => ‘root’,
‘password’ => ‘’,
‘charset’ => ‘utf8’

$app->get(’/’, function ($request, $response) use ($db) {

$data = $db->select(“account”, [

To access the object $db I used -> use ($db) {…
Or to access to variable i used -> use ($db, $variable1, $variable2) { …
Please tell me how to do it in Slim/Skeleton by @akrabat, how to pass the object medoo, or other variable to the class HomeAction.

I’m not familiar with Medoo, but take a look at this example with Eloquent and make the necessary tweaks to use Medoo instead. It should be very similar.

Thank you for a help tflight, is a great example with Eloquent, but how to pass a variable in HomeAction class?

The same way the view and logger objects are being passed to the HomeAction in app/dependencies.php. So you would add your Medoo object to the container, something like this…

$container['db'] = function ($c) { $medoo = new medoo([...]); return $medoo; };

Then your HomeAction factory would look something like this.

return new App\Action\HomeAction($c->get('view'), $c->get('logger'), $c->get('db'));

Thank you tflight for your assistance. I managed to figure out that thing with Medoo object and variables. It was quite easy, I just needed a push into the right direction. Your examples were very helpful. Slim/Skeleton by @akrabat is done extremely well, that’s all I need. Thanks again for your help, keep it up

1 Like