├── .gitignore ├── app ├── App │ ├── Router.php │ └── View.php ├── Controller │ ├── HomeController.php │ └── ProductController.php ├── Hello.php ├── Middleware │ ├── AuthMiddleware.php │ └── Middleware.php └── View │ ├── Home │ └── index.php │ ├── index.php │ ├── login.php │ └── register.php ├── composer.json ├── composer.lock ├── public ├── .htaccess └── index.php └── test └── RegexTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | .idea -------------------------------------------------------------------------------- /app/App/Router.php: -------------------------------------------------------------------------------- 1 | $method, 18 | 'path' => $path, 19 | 'controller' => $controller, 20 | 'function' => $function, 21 | 'middleware' => $middlewares 22 | ]; 23 | } 24 | 25 | public static function run(): void 26 | { 27 | $path = '/'; 28 | if (isset($_SERVER['PATH_INFO'])) { 29 | $path = $_SERVER['PATH_INFO']; 30 | } 31 | 32 | $method = $_SERVER['REQUEST_METHOD']; 33 | 34 | foreach (self::$routes as $route) { 35 | $pattern = "#^" . $route['path'] . "$#"; 36 | if (preg_match($pattern, $path, $variables) && $method == $route['method']) { 37 | 38 | // call middleware 39 | foreach ($route['middleware'] as $middleware){ 40 | $instance = new $middleware; 41 | $instance->before(); 42 | } 43 | 44 | $function = $route['function']; 45 | $controller = new $route['controller']; 46 | // $controller->$function(); 47 | 48 | array_shift($variables); 49 | call_user_func_array([$controller, $function], $variables); 50 | 51 | return; 52 | } 53 | } 54 | 55 | http_response_code(404); 56 | echo 'CONTROLLER NOT FOUND'; 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /app/App/View.php: -------------------------------------------------------------------------------- 1 | "Belajar PHP MVC", 14 | "content" => "Selamat Belajar PHP MVC dari Programmer Zaman Now" 15 | ]; 16 | 17 | View::render('Home/index', $model); 18 | } 19 | 20 | function hello(): void 21 | { 22 | echo "HomeController.hello()"; 23 | } 24 | 25 | function world(): void 26 | { 27 | echo "HomeController.world()"; 28 | } 29 | 30 | function about(): void 31 | { 32 | echo "Author : Eko Kurniawan Khannedy"; 33 | } 34 | 35 | function login(): void 36 | { 37 | $request = [ 38 | "username" => $_POST['username'], 39 | "password" => $_POST['password'] 40 | ]; 41 | 42 | $user = [ 43 | 44 | ]; 45 | 46 | $response = [ 47 | "message" => "Login Sukses" 48 | ]; 49 | // kirimkan response ke view 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /app/Controller/ProductController.php: -------------------------------------------------------------------------------- 1 | 2 |
3 |