简体   繁体   中英

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.

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.

I had the same exception and in short my problem was that a class inherited the constructor of its parent and the parent __constructor wasn't public but protected.

Maybe that can spare someone else hours of research.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM