phpdependency-injectionphp-di

PHP-DI cannot be resolved: Entry cannot be resolved: Parameter xxxxxx of __construct() has no value defined or guessable Full definition


Just throwing this out here because I couldn't find a lot of info on this error and it took me about 2 hours to find it. face palm

In container.php -> DBService defined as:

    DBServiceInterface::class => function (ContainerInterface $c) {
        return new DBService(
            $c->get('settings.database'), 
            $c->get(SessionInterface::class), 
            $c->get(ValidatorInterface::class)
    );
    },

Type: DI\Definition\Exception\InvalidDefinition Message: Entry "PVS\HomeController" cannot be resolved: Entry "PVS\DBService\DBService" cannot be resolved: Parameter $settings of __construct() has no value defined or guessable Full definition: Object ( class = PVS\DBService\DBService lazy = false __construct( $settings = #UNDEFINED# $session = get(PVS\Helpers\Storage\Contracts\SessionInterface) $validator = get(PVS\Validation\Contracts\ValidatorInterface) ) ) Full definition: Object ( class = PVS\HomeController lazy = false __construct( $container = get(Psr\Container\ContainerInterface) $view = get(Slim\Views\Twig) $router = get(Slim\Router) $flash = get(Slim\Flash\Messages) $session = get(PVS\Helpers\Storage\Contracts\SessionInterface) $db = get(PVS\DBService\DBService) ) ) File:

So I started looking for problems in my container or in DBService.php itself. The problem was actually in the controller on the first line of the error message.


Solution

  • Constructor of HomeController was defined as:

    public function __construct (ContainerInterface $container, 
        Twig $view, 
        Router $router, 
        Messages $flash, 
        SessionInterface $session, 
        DBService $db) {  <--- Problem here
    

    I changed it to:

        public function __construct (ContainerInterface $container, 
        Twig $view, 
        Router $router, 
        Messages $flash, 
        SessionInterface $session, 
        DBServiceInterface $db) { <--- 
    

    Notice that I am now calling the Interface instead of the concrete implementation and it matches the DI container posted above.