Added: MVC structure, Router, Views and Controllers for home page and login/signup
This commit is contained in:
0
core/Controller.php
Normal file
0
core/Controller.php
Normal file
28
core/Router.php
Normal file
28
core/Router.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
class Router {
|
||||
private $routes = [];
|
||||
|
||||
public function add($route, $action) {
|
||||
$this->routes[$route] = $action;
|
||||
}
|
||||
|
||||
public function dispatch() {
|
||||
$uri = $_SERVER['REQUEST_URI'];
|
||||
$uri = parse_url($uri, PHP_URL_PATH);
|
||||
|
||||
if (array_key_exists($uri, $this->routes)) {
|
||||
$action = $this->routes[$uri];
|
||||
list($controllerName, $methodName) = explode('@', $action);
|
||||
|
||||
require_once controllers . "{$controllerName}.php";
|
||||
|
||||
$controller = new $controllerName();
|
||||
$controller->$methodName();
|
||||
} else {
|
||||
http_response_code(404);
|
||||
$view = new View();
|
||||
$view->render('errors/404');
|
||||
}
|
||||
}
|
||||
}
|
17
core/View.php
Normal file
17
core/View.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
class View
|
||||
{
|
||||
public function render($view, $data = [], $layout = 'base')
|
||||
{
|
||||
// Extract variables to be accessible in the view
|
||||
extract($data);
|
||||
|
||||
// Capture the view content
|
||||
ob_start();
|
||||
require_once views . $view . '.php';
|
||||
$content = ob_get_clean();
|
||||
|
||||
// Include the base layout and inject the view content
|
||||
require_once views . "layouts/$layout.php";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user