I am setting up a new app using Slim 4 with PHP-DI as my dependency container. Unfortunately I receive an fatal error when attaching the container to the app.
Warning: Cannot bind an instance to a static closure in C:\wamp64\vhosts\app\vendor\slim\slim\Slim\CallableResolver.php on line 175
Fatal error: Uncaught TypeError: Return value of Slim\CallableResolver::bindToContainer() must be callable, null returned in C:\wamp64\vhosts\app\vendor\slim\slim\Slim\CallableResolver.php on line 177
TypeError: Return value of Slim\CallableResolver::bindToContainer() must be callable, null returned in C:\wamp64\vhosts\app\vendor\slim\slim\Slim\CallableResolver.php on line 177
Here is the code:
<?php
use DI\Container;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
// Create Container using PHP-DI
$container = new Container();
// Set container to create App with on AppFactory
AppFactory::setContainer($container);
$app = AppFactory::create();
$app->get('/', static function (Request $request, Response $response, $args) {
$response->getBody()->write('Hello world!');
return $response;
});
$app->run();
When I remove AppFactory::setContainer($container);
the error goes away an “Hello World!” is displayed as expected.
Environment: (WAMPserver) PHP 7.2.14, Apache 2.4.37, PHP-DI 6.0.9, Slim 4.2
I also tried PHP 7.1, Slim 4.1, and Slim 4.0. The stack trace changes slightly but the error is the same.