MyController.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * MyController
  4. *
  5. * Basic DEMO outline for standard controllers
  6. *
  7. * @package MicroMVC
  8. * @author David Pennington
  9. * @copyright (c) 2011 MicroMVC Framework
  10. * @license http://micromvc.com/license
  11. ********************************** 80 Columns *********************************
  12. */
  13. abstract class MyController extends \Micro\Controller
  14. {
  15. // Global view template
  16. public $template = 'Layout';
  17. /**
  18. * Called after the controller is loaded, before the method
  19. *
  20. * @param string $method name
  21. */
  22. public function initialize($method)
  23. {
  24. \Micro\Session::start();
  25. }
  26. /**
  27. * Load database connection
  28. */
  29. public function load_database($name = 'database')
  30. {
  31. // Load database
  32. $db = new \Micro\Database(config()->$name);
  33. // Set default ORM database connection
  34. if(empty(\Micro\ORM::$db))
  35. {
  36. \Micro\ORM::$db = $db;
  37. }
  38. return $db;
  39. }
  40. /**
  41. * Show a 404 error page
  42. */
  43. public function show_404()
  44. {
  45. headers_sent() OR header('HTTP/1.0 404 Page Not Found');
  46. $this->content = new \Micro\View('404');
  47. }
  48. /**
  49. * Save user session and render the final layout template
  50. */
  51. public function send()
  52. {
  53. \Micro\Session::save();
  54. headers_sent() OR header('Content-Type: text/html; charset=utf-8');
  55. $layout = new \Micro\View($this->template);
  56. $layout->set((array) $this);
  57. print $layout;
  58. $layout = NULL;
  59. if(config()->debug_mode)
  60. {
  61. print new \Micro\View('System/Debug');
  62. }
  63. }
  64. }
  65. // End