Advertisement
rhuntington

Example mvc

Jul 6th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.26 KB | None | 0 0
  1. // root index.php
  2.  
  3. /* auto loads all classes and instantiates the app */
  4.  
  5. <?php
  6.  
  7.     function __autoload($model_classes) {
  8.         require_once './server/models/'.$model_classes.'.php';
  9.     }
  10.  
  11.     $test = new Test();
  12.     $test->setRoutes();
  13.  
  14. ?>
  15.  
  16. // ./models/Test.php
  17.  
  18. /*
  19. Main app page where routes are declared and loads the views
  20. */
  21.  
  22. <?php
  23.     // set routes here
  24.     class Test {
  25.         function setRoutes() {
  26.             Route::set('index.php', function() {
  27.                 View::make('index');
  28.             });
  29.         }
  30.     }
  31.  
  32. ?>
  33.  
  34. <?php
  35.  
  36.     function __autoload($model_classes) {
  37.         require_once './server/models/'.$model_classes.'.php';
  38.     }
  39.  
  40.     $test = new Test();
  41.     $test->setRoutes();
  42.  
  43. ?>
  44.  
  45. // ./models/Route.php
  46.  
  47. /*
  48.  
  49. Creates route setting method and verifies route location
  50. Invoking the $function causes the route to be intantiated
  51.  
  52. */
  53.  
  54.  
  55. <?php
  56.  
  57.     class Route {
  58.  
  59.         public static $validRoutes = array();
  60.        
  61.         public static function set($route, $function) {
  62.             if($_GET['url'] === $route) {
  63.                 self::$validRoutes = $route;
  64.                 $function->__invoke();
  65.             }
  66.         }
  67.     }
  68.  
  69. ?>
  70.  
  71. // ./models/View.php
  72.  
  73. /*
  74. Creates the view and calls on it's controller (if any)
  75. */
  76.  
  77. <?php
  78.  
  79.     class View {
  80.         public static function make($view) {
  81.             require_once './server/controllers/'.$view.'.php';
  82.             require_once './server/views/'.$view.'.php';
  83.             return 1;
  84.         }
  85.     }
  86.  
  87. ?>
  88.  
  89. // ./models/Data.php
  90.  
  91. /*
  92. Currently just returns data that was entered on the front end
  93. This will be rewritten to better handle AJAX calls
  94. */
  95.  
  96. <?php
  97.  
  98.     class Data {
  99.  
  100.         public static function post($data) {
  101.             header('Content-type: application/json');
  102.             echo json_encode($data);
  103.         }
  104.        
  105.     }
  106. ?>
  107.  
  108. // ./controllers/index.php
  109.  
  110. /*
  111. Handles the logic and will interface eventually to the db
  112. */
  113.  
  114. <?php
  115.  
  116.     class Home {
  117.  
  118.         public static function setSession($user) {
  119.             session_start();
  120.             $_SESSION['user'] = $user;
  121.         }
  122.  
  123.         public static function getData() {
  124.             if(isset($_POST['input'])) {
  125.                 return Data::post($_POST['input']);
  126.             }
  127.         }
  128.     }
  129.     echo Home::getData();
  130. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement