My DI declaration:
$di = new FactoryDefault();
$di->setShared('session', function () {
$session = new SessionAdapter();
$session->start();
return $session;
});
$di->set('url', function () use ($config) {
$url = new UrlResolver();
$url->setBaseUri($config->application->baseUri);
return $url;
}, true);
$di->set('config', $config, true);
$di->setShared('view', function () use ($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines(array(
'.volt' => function ($view, $di) use ($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array(
'compiledPath' => $config->application->cacheDir,
'compiledSeparator' => '_'
));
return $volt;
},
'.phtml' => 'Phalcon\Mvc\View\Engine\Php'
));
return $view;
});
$di->set('db', function () use ($config) {
return new DbAdapter($config->database->toArray());
});
$di->set('modelsMetadata', function () {
return new MetaDataAdapter();
});
$di->set('security', function () {
$security = new Security();
$security->setWorkFactor(12);
return $security;
}, true);
$di->set('message', function () {
$flash = new Flash([
'error' => 'alert alert-danger',
'success' => 'alert alert-success',
'notice' => 'alert alert-info',
]);
return $flash;
});
$di->set('dispatcher', function () {
$event = new EventsManager();
$event->attach("dispatch:beforeExecuteRoute", new Controle);
$event->attach('dispatch:beforeException', new Controle);
$event->attach('dispatch:beforeNotFoundAction', new Controle);
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace("Ctrle\\Controller");
$dispatcher->setEventsManager($event);
return $dispatcher;
});
$di->set('log', function() use ($config){
return new Log($config->application->logDir);
}, true);
$di->set('param', function() use ($config){
return new Parametros();
}, true);
$di->set('mail', function() use ($config){
return new Mail();
}, true);