index.php 3.0 KB

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