Guest User

Untitled

a guest
Dec 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. <?php
  2. namespace Application\Source;
  3. # The public index will require this file first,
  4. /*
  5. * ROUTER FILE
  6. */
  7. class RouterClass
  8. {
  9. public static function init()
  10. {
  11. # Enter your routes into the array below.
  12. # Relative url path as key => an array containing controller and method as its value.
  13. $route = array(
  14. "/" => array('MainController', 'index'),
  15. "/home" => array('MainController', 'home'),
  16. "/getArticle" => array('MainController', 'foo'),
  17. "/insert" => array('MainController', 'logSomething')
  18. );
  19. //*************************************************************************************************
  20. //___________________________________Matching below________________________________________________
  21. # Detects the url that is trying to be accesed and checks the $route array for the matching key.
  22. foreach ($route as $route_key => $action)
  23. {
  24. $class_select = $action[0];
  25. $controller_full_path = dirname(__DIR__).'/controllers/'.$class_select.'.php';
  26. $method = $action[1];
  27. require_once($controller_full_path);
  28. if($_SERVER['REQUEST_URI'] == $route_key)
  29. {
  30. try
  31. {
  32. # So, apparently if you declare the namespace as string it doesn't bring
  33. # error while trying the normal way with variable raises error.
  34. $class_select = 'Application\Controller\\'.$class_select;
  35. #$class_select::$method();
  36. $active_controller = new $class_select();
  37. $active_controller->$method();
  38. }
  39. catch (\Exception $e)
  40. {
  41. if($_SERVER['PHP_SELF'] == $route_key)
  42. {
  43. try
  44. {
  45. $class_select::$method();
  46. }
  47. catch(\Exception $e)
  48. {
  49. $e->getMessage(); # DEBUG only, will return a generic error view.
  50. }
  51. }
  52. $e->getMessage(); # DEBUG only, will return a generic error view.
  53. }
  54. }
  55. }
  56. }
  57. }
Add Comment
Please, Sign In to add comment