WebAppController.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace KarmaFW\Routing\Controllers;
  3. use \KarmaFW\WebApp;
  4. use \KarmaFW\Lib\Hooks\HooksManager;
  5. class WebAppController extends AppController
  6. {
  7. protected $request_uri = null;
  8. protected $request_method = null;
  9. protected $route = null;
  10. protected $template;
  11. protected $user_id;
  12. protected $flash;
  13. public function __construct($request_uri=null, $request_method=null, $route=null)
  14. {
  15. parent::__construct($request_uri, $request_method, $route);
  16. if (defined('USE_HOOKS') && USE_HOOKS) {
  17. HooksManager::applyHook('webcontroller.before', [$this]);
  18. }
  19. $this->user_id = session('user_id');
  20. $this->flash = session('flash');
  21. $_SESSION['flash'] = []; // ['success' => 'action done !', 'error' => 'an error occured', 'warning' => 'notice ...']
  22. if (defined('TPL_DIR')) {
  23. $this->template = WebApp::createTemplate();
  24. $this->template->assign('user_id', $this->user_id);
  25. $this->template->assign('flash', $this->flash);
  26. if (defined('APP_NAME')) {
  27. $this->template->assign('meta_title', APP_NAME);
  28. $this->template->assign('meta_description', APP_NAME);
  29. $this->template->assign('h1', APP_NAME);
  30. }
  31. }
  32. if (defined('USE_HOOKS') && USE_HOOKS) {
  33. HooksManager::applyHook('webcontroller.after', [$this]);
  34. }
  35. }
  36. public function getRoute()
  37. {
  38. return $this->route;
  39. }
  40. public function getRequestMethod()
  41. {
  42. return $this->request_method;
  43. }
  44. public function getRequestUri()
  45. {
  46. return $this->request_uri;
  47. }
  48. public function getTemplate()
  49. {
  50. return $this->template;
  51. }
  52. public function error($http_status, $meta_title=null, $h1=null, $message=null)
  53. {
  54. return WebApp::error($http_status, $meta_title, $h1, $message);
  55. }
  56. public function error400($title = 'Bad request', $message = '')
  57. {
  58. return $this->error(400, $title, $title, $message);
  59. }
  60. public function error403($title = 'Forbidden', $message = 'you are not allowed')
  61. {
  62. return $this->error(403, $title, $title, $message);
  63. }
  64. public function error404($title = 'Page not found', $message = "The page you're looking for doesn't exist")
  65. {
  66. return $this->error(404, $title, $title, $message);
  67. }
  68. public function error500($title = 'Internal Server Error', $message = 'An error has occured')
  69. {
  70. return $this->error(500, $title, $title, $message);
  71. }
  72. public function error503($title = 'Service Unavailable', $message = 'The service is unavailable')
  73. {
  74. return $this->error(503, $title, $title, $message);
  75. }
  76. }