I’m working on a simple project with Slim 4, where I’m using PHP-DI 7 as my dependency injection container. Until the point where I implemented interfaces in my code, the project was working fine. However, after implementing interfaces like UserRepositoryInterface and its implementation, dependency injection stopped working. I’ve done quite a bit of research, both in Slim’s documentation and PHP-DI’s documentation, but I couldn’t find anything to solve the problem. I’ve tried some other methods for declaring the definitions as you can see in the comments, but without success. Have you ever experienced this? If so, how did you fix it? I’m open to suggestions for my code. Below is the error in question:
Fatal error: Uncaught DI\Definition\Exception\InvalidDefinition: Entry "Masoretic\Controllers\HelloWorldController" cannot be resolved: Entry "Masoretic\Businesses\User\UserBusinessInterface" cannot be resolved: the class is not instantiable Full definition: Object ( class = #NOT INSTANTIABLE# Masoretic\Businesses\User\UserBusinessInterface lazy = false ) Full definition: Object ( class = Masoretic\Controllers\HelloWorldController lazy = false __construct( $userBusiness = get(Masoretic\Businesses\User\UserBusinessInterface) ) ) in /var/www/html/vendor/php-di/php-di/src/Definition/Exception/InvalidDefinition.php:19 Stack trace: #0 /var/www/html/vendor/php-di/php-di/src/Definition/Resolver/ObjectCreator.php(139): DI\Definition\Exception\InvalidDefinition::create(Object(DI\Definition\ObjectDefinition), 'Entry "Masoreti...') #1 /var/www/html/vendor/php-di/php-di/src/Definition/Resolver/ObjectCreator.php(56): DI\Definition\Resolver\ObjectCreator->createInstance(Object(DI\Definition\ObjectDefinition), Array) #2 /var/www/html/vendor/php-di/php-di/src/Definition/Resolver/ResolverDispatcher.php(60): DI\Definition\Resolver\ObjectCreator->resolve(Object(DI\Definition\ObjectDefinition), Array) #3 /var/www/html/vendor/php-di/php-di/src/Container.php(353): DI\Definition\Resolver\ResolverDispatcher->resolve(Object(DI\Definition\ObjectDefinition), Array) #4 /var/www/html/vendor/php-di/php-di/src/Container.php(136): DI\Container->resolveDefinition(Object(DI\Definition\ObjectDefinition)) #5 /var/www/html/vendor/slim/slim/Slim/CallableResolver.php(129): DI\Container->get('Masoretic\\Contr...') #6 /var/www/html/vendor/slim/slim/Slim/CallableResolver.php(90): Slim\CallableResolver->resolveSlimNotation('Masoretic\\Contr...') #7 /var/www/html/vendor/slim/slim/Slim/CallableResolver.php(63): Slim\CallableResolver->resolveByPredicate('Masoretic\\Contr...', Array, 'handle') #8 /var/www/html/vendor/slim/slim/Slim/Routing/Route.php(340): Slim\CallableResolver->resolveRoute('Masoretic\\Contr...') #9 /var/www/html/vendor/slim/slim/Slim/MiddlewareDispatcher.php(65): Slim\Routing\Route->handle(Object(Slim\Psr7\Request)) #10 /var/www/html/vendor/slim/slim/Slim/MiddlewareDispatcher.php(65): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #11 /var/www/html/vendor/slim/slim/Slim/Routing/Route.php(315): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #12 /var/www/html/vendor/slim/slim/Slim/Routing/RouteRunner.php(68): Slim\Routing\Route->run(Object(Slim\Psr7\Request)) #13 /var/www/html/vendor/slim/slim/Slim/MiddlewareDispatcher.php(65): Slim\Routing\RouteRunner->handle(Object(Slim\Psr7\Request)) #14 /var/www/html/vendor/slim/slim/Slim/App.php(199): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #15 /var/www/html/vendor/slim/slim/Slim/App.php(183): Slim\App->handle(Object(Slim\Psr7\Request)) #16 /var/www/html/public/index.php(20): Slim\App->run() #17 {main} thrown in /var/www/html/vendor/php-di/php-di/src/Definition/Exception/InvalidDefinition.php on line 19
Here is my defitions of the PHP-DI:
<?php
declare(strict_types=1);
use Masoretic\Businesses\User\UserBusiness;
use Masoretic\Businesses\User\UserBusinessInterface;
use Masoretic\Controllers\HelloWorldController;
use Masoretic\DBAL\Configurations;
use Masoretic\DBAL\Database;
use Masoretic\Models\User;
use Masoretic\Repositories\User\UserRepository;
use Masoretic\Repositories\User\UserRepositoryInterface;
use function DI\create;
return [
'HelloWorldController' => create(HelloWorldController::class),
'Configurations' => create(Configurations::class),
'Database' => create(Database::class),
'User' => create(User::class),
// 'UserRepositoryInterface' => create(UserRepository::class),
// 'UserBusinessInterface' => create(UserBusiness::class),
'UserRepository' => create(UserRepository::class),
'UserRepositoryInterface' => create(UserRepositoryInterface::class),
'UserBusiness' => create(UserBusiness::class),
'UserBusinessInterface' => create(UserBusinessInterface::class),
];