index.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. define('APP_PATH', realpath('..'));
  3. require APP_PATH . "/vendor/autoload.php";
  4. try {
  5. // Load the config
  6. $config = include APP_PATH . "/app/config/config.php";
  7. // Register an autoloader
  8. $loader = new \Phalcon\Loader();
  9. $loader->registerDirs(array(
  10. $config->application->controllersDir,
  11. $config->application->modelsDir
  12. ))->register();
  13. // Create a DI
  14. $di = new Phalcon\DI\FactoryDefault();
  15. // Setting up the router
  16. $di->set('router', require APP_PATH . '/app/config/routes.php');
  17. //MetaData
  18. $di->set('modelsMetadata', function(){
  19. if (function_exists('apc_store')) {
  20. return new Phalcon\Mvc\Model\MetaData\Apc();
  21. } else {
  22. return new Phalcon\Mvc\Model\MetaData\Memory(array(
  23. 'metaDataDir' => APP_PATH . "/app/compiled-templates/"
  24. ));
  25. }
  26. });
  27. // Setting up the view component (seems to be required even when not used)
  28. $di->set('view', function() use ($config) {
  29. $view = new \Phalcon\Mvc\View();
  30. $view->setViewsDir($config->application->viewsDir);
  31. $view->registerEngines(array(
  32. ".volt" => function($view) {
  33. $volt = new \Phalcon\Mvc\View\Engine\Volt($view);
  34. $volt->setOptions(array(
  35. "path" => APP_PATH . "/app/compiled-templates/",
  36. "extension" => ".compiled",
  37. "separator" => '_',
  38. ));
  39. return $volt;
  40. }
  41. ));
  42. return $view;
  43. });
  44. // Setting up the database connection
  45. $di->set('db', function() use ($config) {
  46. $database = $config->database;
  47. return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
  48. 'host' => $database->host,
  49. 'username' => $database->username,
  50. 'password' => $database->password,
  51. 'dbname' => $database->name,
  52. 'options' => array(
  53. PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
  54. PDO::ATTR_PERSISTENT => true,
  55. )
  56. ));
  57. });
  58. // Setting up the mongodb connection
  59. $di->set('mongo', function() use ($config) {
  60. $mongodbConfig = $config->mongodb;
  61. $mongo = new \Phalcon\Db\Adapter\MongoDB\Client($mongodbConfig->url);
  62. return $mongo->selectDatabase($mongodbConfig->db);
  63. });
  64. //Registering the collectionManager service
  65. $di->set('collectionManager', function() {
  66. // Setting a default EventsManager
  67. $modelsManager = new Phalcon\Mvc\Collection\Manager();
  68. $modelsManager->setEventsManager(new Phalcon\Events\Manager());
  69. return $modelsManager;
  70. }, true);
  71. // Handle the request
  72. $request = new Phalcon\Http\Request();
  73. $application = new \Phalcon\Mvc\Application();
  74. $application->setDI($di);
  75. $application->handle($request->getURI())->send();
  76. } catch(\Phalcon\Exception $e) {
  77. echo "PhalconException: ", $e->getMessage();
  78. }