index.php 2.8 KB

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