rhuntington icon

Example mvc

rhuntington | PRO | 07/06/19 06:28:11 AM UTC | 0 ⭐ | 296 👁️ | Never ⏰ | []
PHP |

2.26 KB

|

None

|

0 👍

/

0 👎

// root index.php
 
/* auto loads all classes and instantiates the app */
 
<?php
 
    function __autoload($model_classes) {
        require_once './server/models/'.$model_classes.'.php';
    }
 
    $test = new Test();
    $test->setRoutes();
 
?>
 
// ./models/Test.php
 
/*
Main app page where routes are declared and loads the views
*/
 
<?php
    // set routes here
    class Test {
        function setRoutes() {
            Route::set('index.php', function() {
                View::make('index');
            });
        }
    }
 
?>
 
<?php
 
    function __autoload($model_classes) {
        require_once './server/models/'.$model_classes.'.php';
    }
 
    $test = new Test();
    $test->setRoutes();
 
?>
 
// ./models/Route.php
 
/* 
 
Creates route setting method and verifies route location
Invoking the $function causes the route to be intantiated
 
*/
 
 
<?php
 
    class Route {
 
        public static $validRoutes = array();
        
        public static function set($route, $function) {
            if($_GET['url'] === $route) {
                self::$validRoutes = $route;
                $function->__invoke();
            }
        }
    }
 
?>
 
// ./models/View.php
 
/*
Creates the view and calls on it's controller (if any)
*/
 
<?php
 
    class View {
        public static function make($view) {
            require_once './server/controllers/'.$view.'.php';
            require_once './server/views/'.$view.'.php';
            return 1;
        }
    }
 
?>
 
// ./models/Data.php
 
/*
Currently just returns data that was entered on the front end
This will be rewritten to better handle AJAX calls
*/
 
<?php
 
    class Data {
 
        public static function post($data) {
            header('Content-type: application/json');
            echo json_encode($data);
        }
        
    }
?>
 
// ./controllers/index.php
 
/*
Handles the logic and will interface eventually to the db
*/
 
<?php
 
    class Home {
 
        public static function setSession($user) {
            session_start();
            $_SESSION['user'] = $user;
        }
 
        public static function getData() {
            if(isset($_POST['input'])) {
                return Data::post($_POST['input']);
            }
        }
    }
    echo Home::getData();
?>

Comments