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:

<?php

    $loader = new \Phalcon\Loader();

    $loader->registerDirs(array(
    __DIR__ . '/config/',
    __DIR__ . '/models/',
    __DIR__ . '/plugins/',

    ))->register();

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

But it does not work this way.

Please advise, is there a possible solution in MicroApp?

Could you declare dependency injection? My index.php

<?php

error_reporting(E_ALL);

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,
                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                ],
            ]);
    });

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

    /*
     * Registering an autoloader
     */
    $loader = new Phalcon\Loader();
    $loader->registerDirs([
        $config->phalcon->controllersDir,
        $config->phalcon->listenersDir,
        $config->phalcon->modelsDir,
        $config->phalcon->validationsDir,
    ])->register();
    //Add namespace to Phalcon
    $loader->registerNamespaces([
        '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->setPrefix('/v1/ping');
    $ping->map('/', 'indexAction');

    /*
     * Handle the request
     */
    $app = new Phalcon\Mvc\Micro();
    // Add di
    $app->setDI($di);
    // Bind the events manager to the app
    $app->setEventsManager($eventManager);
    // Bind API
    $app->mount($ping);
    // 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!');
        $app->response->send();
    });
    $app->handle();
} catch (Phalcon\Exception $e) {
    echo $e->getMessage();
} catch (PDOException $e) {
    echo $e->getMessage();
}


14.9k
Accepted
answer
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: