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

Rest API - does not work with config folder

I 've used tutorial on a simple rest api to create one and now i would like to place services to config folder, routes to routes.php under config folder as in the general application(not micro), to make index.php like this:


    $loader = new \Phalcon\Loader();

    __DIR__ . '/config/',
    __DIR__ . '/models/',
    __DIR__ . '/plugins/',


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

But it does not work this way.

Please advise, is there a possible solution in MicroApp?

Could you declare dependency injection? My index.php



try {

     * Read the configuration
    $config = new Phalcon\Config\Adapter\Ini(__DIR__.'/../app/config/config.ini');

     * Set dependency injection
    $di = new Phalcon\DI\FactoryDefault();

     * Database connection is created based in the parameters defined in the configuration file
    $di->set('db', function () use ($config) {
        return new Phalcon\Db\Adapter\Pdo\Mysql(
                'host' => $config->database->host,
                'username' => $config->database->username,
                'password' => $config->database->password,
                'dbname' => $config->database->dbname,
                'options' => [
                    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'",
                    PDO::ATTR_CASE => PDO::CASE_LOWER,

     * Set response to di
    $di->set('response', function () {
        return new Phalcon\Http\Response();

     * Registering an autoloader
    $loader = new Phalcon\Loader();
    //Add namespace to Phalcon
        'Cms' => $config->phalcon->modelsCmsDir,

     * Event Manager
    //Create a events manager
    $eventManager = new Phalcon\Events\Manager();
    //Listen all the application events
    $eventManager->attach('micro:beforeExecuteRoute', new AuthenticationListener());
    $eventManager->attach('micro:afterExecuteRoute', new HeaderListener());

     * Ping API
    $ping = new Phalcon\Mvc\Micro\Collection();
    //Set the main handler. ie. a controller instance
    $ping->setHandler('PingController', true);
    //Set a common prefix for all routes
    $ping->map('/', 'indexAction');

     * Handle the request
    $app = new Phalcon\Mvc\Micro();
    // Add di
    // Bind the events manager to the app
    // Bind API
    // Handle bad request
    $app->notFound(function () use ($app) {
        $app->response->setStatusCode(404, 'Not Found')->sendHeaders();
        $app->response->setContent('This is crazy, but this page was not found!');
} catch (Phalcon\Exception $e) {
    echo $e->getMessage();
} catch (PDOException $e) {
    echo $e->getMessage();

edited Jun '15

No, DI did not work for me when i placed it outside of index.php.

Thanx be to Phalcon, there is a way to create correct structure, when you use dev tools to create project. I used create-project and parameter "micro" and got correct structure right away:

The index.php in public Dir is as follows: