WebAppController.php 2.1 KB

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