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

Dependency injection container is required

Hi,

I followed the REST tutorial, but i can't get it to work.

curl -i -X GET https://127.0.1.1/phalcontest/api/user

I keep getting this error :

[Fri Nov 28 17:45:32.336974 2014] [:error] [pid 6208] [client 127.0.0.1:44038] PHP Fatal error: Uncaught exception 'Phalcon\Mvc\Micro\Exception' with message 'A dependency injection container is required to access related dispatching services' in /var/www/phalcontest/index.php:48\nStack trace:\n#0 /var/www/phalcontest/index.php(48): Phalcon\Mvc\Micro->handle()\n#1 {main}\n thrown in /var/www/phalcontest/index.php on line 48

And here is my index.php

 <?php

 $loader = new \Phalcon\Loader();
 $loader->registerDirs(array(__DIR__ . '/Models/'))->register();

 $di = new \Phalcon\DI\FactoryDefault();
 $di->set('db', function(){
     return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
         "host" => "localhost",
        "username" => "root",
        "password" => "secret",
        "dbname" => "babyv2"
    ));
});

$app = new \Phalcon\Mvc\Micro($di);

/*
 * Routes definitions
 */

$app->get('/api/user', function() use ($app) {
    $users = User::getFullList();

    echo json_encode($users);
});

$app->get('/api/user/search/{name}', function($name) use ($app) {
    // Finds a user by name
});

$app->get('/api/user/{id:[0-9]+}', function($id) use ($app) {
    // Finds a user by id
});

$app->post('/api/user', function() use ($app) {
    // Adds a new user
});

$app->put('/api/user/{id:[0-9]+}', function() use ($app) {
    // Updates user based on primary key
});

$app->delete('/api/game/{id:[0-9]+}', function() use ($app) {
    // Deletes game based on primary key
});

$app->handle();

Any anwser is welcome... Thanks.

I think you should set DI to your application's instance before calling handle()

$app->setDI($di);
$app->handle();

It didn't change anything...