index.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. try {
  3. // Load the config
  4. $config = include(__DIR__."/../app/config/config.php");
  5. // Register an autoloader
  6. $loader = new \Phalcon\Loader();
  7. $loader->registerDirs(array(
  8. $config->application->controllersDir,
  9. $config->application->modelsDir
  10. ))->register();
  11. // Create a DI
  12. $di = new Phalcon\DI\FactoryDefault();
  13. // Setting up the router
  14. $di->set('router', function() use ($config) {
  15. return include($config->application->routes);
  16. });
  17. //Register Volt as a service
  18. $di->set('voltService', function($view, $di) {
  19. $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
  20. $volt->setOptions(array(
  21. "compiledPath" => "../app/compiled-templates/",
  22. "compiledExtension" => ".compiled"
  23. ));
  24. return $volt;
  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" => 'voltService'
  32. ));
  33. return $view;
  34. });
  35. // Setting up the database connection
  36. $di->set('db', function() use ($config) {
  37. return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
  38. 'host' => $config->database->host,
  39. 'username' => $config->database->username,
  40. 'password' => $config->database->password,
  41. 'dbname' => $config->database->name,
  42. 'options' => array(
  43. PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
  44. )
  45. ));
  46. });
  47. // Handle the request
  48. $application = new \Phalcon\Mvc\Application();
  49. $application->setDI($di);
  50. echo $application->handle()->getContent();
  51. } catch(\Phalcon\Exception $e) {
  52. echo "PhalconException: ", $e->getMessage();
  53. }