Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. <?php
  2.  
  3. // Run application
  4. require 'application/app.php';
  5. $app = new App();
  6. $app->run();
  7.  
  8. <?php
  9.  
  10. class App {
  11.  
  12. public function run() {
  13. // Determine request path
  14. $path = $_SERVER['REQUEST_URI'];
  15.  
  16. // Load routes
  17. require_once 'routes.php';
  18.  
  19. // Match this request to a route
  20. if(isset(Routes::$routes[$path])) {
  21.  
  22. } else {
  23. // Use default route
  24. $controller = Routes::$routes['/'][0];
  25. $action = Routes::$routes['/'][1];
  26. }
  27.  
  28. // Check if controller exists
  29. if(file_exists('controllers/' . $controller . '.php')) {
  30. // Include and instantiate controller
  31. require_once 'controllers/' . $controller . '.php';
  32. $controller = new $controller . 'Controller';
  33.  
  34. // Run method for this route
  35. if(method_exists($controller, $action)) {
  36. return $controller->$action();
  37. } else {
  38. die('Method ' . $action . ' missing in controller ' . $controller);
  39. }
  40. } else {
  41. die('Controller ' . $controller . 'Controller missing');
  42. }
  43. }
  44.  
  45. }
  46.  
  47. <?php
  48.  
  49. class Routes {
  50.  
  51. public static $routes = array(
  52. '/' => array('Pages', 'home')
  53. );
  54.  
  55. }
  56.  
  57. /application
  58. /controllers
  59. Pages.php
  60. /models
  61. /views
  62. app.php
  63. routes.php
  64.  
  65. // Run application
  66. define('ROOT', dirname(__FILE__) );
  67. require ROOT . '/application/app.php';
  68. $app = new App();
  69. $app->run();
  70.  
  71. // Check if controller exists
  72. if(file_exists(ROOT . '/application/controllers/' . $controller . '.php')) {
  73. // Include and instantiate controller
  74. require_once ROOT. '/application/controllers/' . $controller . '.php';
  75. $controller = new $controller . 'Controller';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement