We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Problems with Phalcon DI configuration

I'm having some problems with the dependency injection. Since i've updated phalcon incubator from 1.2.4 to 1.3.0, i can't run my unit tests anymore. These messages appears:

Phalcon\DI\Exception: Service 'collectionManager' was not found in the dependency injection container

or

Phalcon\DI\Exception: Service 'Mysql1' was not found in the dependency injection container

I know I have to set my collectionManager(i did), but it doens't work.

Can anyone explain me how to do it correctly?

my TestHelper.php


$config = new \Phalcon\Config(array (
'database'    => array(
        'mysql1' => array(
            'host'     => 'localhost',
            'username' => 'root',
            'password' => 'root',
            'dbname'   => 'test',
        ),
        'mongo'  => array(
            'host'     => 'localhost',
            'username' => 'root',
            'password' => 'root',
            'dbname'   => 'test',
        )
    ),
));

$di->set('collectionManager', function () {
    return new \Phalcon\Mvc\Collection\Manager();
}, true);

// mysql database
$di->set('Mysql1', function () use ($config) {
$conn = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host"     => $config->database->mysql1->host,
        "username" => $config->database->mysql1->username,
        "password" => $config->database->mysql1->password,
        "dbname"   => $config->database->mysql1->dbname,
        "options"  => array(
            PDO::ATTR_AUTOCOMMIT => 0
        )
    ));
    return $conn;
});

//MongoDB Database 
$di->set('MongoDB', function () use ($config) {
    if (!$config->database->mongo->username OR !$config->database->mongo->password) {
        $mongo = new MongoClient('mongodb://' . $config->database->mongo->host);
    } else {
        $mongo = new MongoClient("mongodb://" . $config->database->mongo->username . ":" . $config->database->mongo->password . "@" . $config->database->mongo->host, array("db" => $config->database->mongo->dbname));
    }

    return $mongo->selectDb($config->database->mongo->dbname);
}, TRUE);


22.6k

Make sure that you've actually set the DependencyInjector you've been working on when you create your app as well:

<?php

$di->set('collectionManager', function() use ($config) {
    // ...
}

$application = new \Phalcon\Mvc\Application();
$application->setDi($di); // This is the line I'm referring to.
echo $application->handle()->getContent();


4.1k

Well,

I did it exactly as you said, but still not working. now phalcon throws this message:

Fatal error: Uncaught exception 'LogicException' with message 'Unexpected value type: expected object implementing Phalcon\Mvc\ViewInterface, object of type Phalcon\Mvc\View\Simple given' in /var/www/html/myproject on line 194 (the line of getContent() )

$di->set('collectionManager', function () {
    return new \Phalcon\Mvc\Collection\Manager();
}, TRUE);

Looks like, from the above mentioned error, your next place to look at is your "view" setup in the $ di. Not the collectionManager.



2.5k

Marcello, try the following:

$application = new \Phalcon\Mvc\Application($di);
$application->useImplicitView(false);
echo $application->handle()->getContent();

Well,

I did it exactly as you said, but still not working. now phalcon throws this message:

Fatal error: Uncaught exception 'LogicException' with message 'Unexpected value type: expected object implementing Phalcon\Mvc\ViewInterface, object of type Phalcon\Mvc\View\Simple given' in /var/www/html/myproject on line 194 (the line of getContent() )

$di->set('collectionManager', function () {
   return new \Phalcon\Mvc\Collection\Manager();
}, TRUE);