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();
}