Here is the bootstrap code:
<?php
error_reporting(E_ALL);
try {
/**
* Read the configuration
*/
$config = include __DIR__ . "/../app/config/config.php";
/**
* Read auto-loader
*/
include __DIR__ . "/../app/config/loader.php";
/**
* Read services
*/
include __DIR__ . "/../app/config/services.php";
/**
* Handle the request
*/
$application = new \Phalcon\Mvc\Application($di);
echo $application->handle()->getContent();
} catch (\Exception $e) {
echo $e->getMessage();
}
AND Here is Services.php:
<?php
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Db\Adapter\Pdo\Sqlite as DbAdapter;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
/**
* The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
*/
$di = new FactoryDefault();
/**
* The URL component is used to generate all kind of urls in the application
*/
$di->set('url', function () use ($config) {
$url = new UrlResolver();
$url->setBaseUri($config->application->baseUri);
return $url;
}, true);
/**
* Setting up the view component
*/
$di->set('view', function () use ($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines(array(
'.phtml' => 'Phalcon\Mvc\View\Engine\Php'
));
return $view;
}, true);
/**
* Database connection for Sqlite based on config.php
*/
$di->set('db', function () use ($config) {
return new DbAdapter(array(
'dbname' => $config->database->dbname
));
});
/**
* If the configuration specify the use of metadata adapter use it or use memory otherwise
*/
$di->set('modelsMetadata', function () {
return new MetaDataAdapter();
});
/**
* Start the session the first time some component request the session service
*/
$di->set('session', function () {
$session = new SessionAdapter();
$session->start();
return $session;
});
/**
* Create a models cache for frequently used static data
*/
$di->set('modelsCache',function() {
//Create a Data frontend and set a default lifetime to 1 day
$frontend = new Phalcon\Cache\Frontend\Data(array(
'lifetime' => 86400
));
// Set up APC Cache
$cache = new Phalcon\Cache\Backend\Apc($frontend, array(
'prefix' => 'cache'
));
return $cache;
});
/**
* Create a regular cache for frequently used static data
*/
$di->set('appCache',function() {
//Create a Data frontend and set a default lifetime to 1 day
$frontend = new Phalcon\Cache\Frontend\Data(array(
'lifetime' => 86400
));
// Set up APC Cache
$cache = new Phalcon\Cache\Backend\Apc($frontend, array(
'prefix' => 'appcache'
));
return $cache;
});
/**
* Take control of dispatcher in order to override EventManager methods
*/
$di->set('dispatcher', function() use ($di) {
//Obtain the standard eventsManager from the DI
$eventsManager = $di->getShared('eventsManager');
//Instantiate the Security plugin
$security = new Security($di);
//Listen for events produced in the dispatcher using the Security plugin
$eventsManager->attach('dispatch', $security);
$dispatcher = new Phalcon\Mvc\Dispatcher();
//Bind the EventsManager to the Dispatcher
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});