index-mongo.php 3.0 KB

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