Initial commit of LightMVC framework files.

This commit is contained in:
2026-07-28 22:57:00 +05:30
parent 6beffc9fe9
commit f8411accb4
17 changed files with 424 additions and 3 deletions
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace Core;
class Application
{
public static string $ROOT_DIR;
public Router $router;
public Request $request;
public Response $response;
public View $view;
public static Application $app;
public function __construct($rootPath)
{
self::$ROOT_DIR = $rootPath;
self::$app = $this;
$this->request = new Request();
$this->response = new Response();
$this->view = new View();
$this->router = new Router($this->request, $this->response);
}
public function run()
{
echo $this->router->resolve();
}
}
+11
View File
@@ -0,0 +1,11 @@
<?php
namespace Core;
class Controller
{
public function render($view, $params = [])
{
return Application::$app->router->renderView($view, $params);
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace Core;
class Request
{
public function getPath()
{
$path = $_SERVER['REQUEST_URI'] ?? '/';
$position = strpos($path, '?');
if ($position === false) {
return $path;
}
return substr($path, 0, $position);
}
public function getMethod()
{
return strtolower($_SERVER['REQUEST_METHOD']);
}
public function getBody()
{
$body = [];
if ($this->getMethod() === 'get') {
foreach ($_GET as $key => $value) {
$body[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_SPECIAL_CHARS);
}
}
if ($this->getMethod() === 'post') {
foreach ($_POST as $key => $value) {
$body[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
}
}
return $body;
}
}
+11
View File
@@ -0,0 +1,11 @@
<?php
namespace Core;
class Response
{
public function setStatusCode(int $code)
{
http_response_code($code);
}
}
+53
View File
@@ -0,0 +1,53 @@
<?php
namespace Core;
class Router
{
protected array $routes = [];
public Request $request;
public Response $response;
public function __construct(Request $request, Response $response)
{
$this->request = $request;
$this->response = $response;
}
public function get($path, $callback)
{
$this->routes['get'][$path] = $callback;
}
public function post($path, $callback)
{
$this->routes['post'][$path] = $callback;
}
public function resolve()
{
$path = $this->request->getPath();
$method = $this->request->getMethod();
$callback = $this->routes[$method][$path] ?? false;
if ($callback === false) {
$this->response->setStatusCode(404);
return $this->renderView("_404");
}
if (is_string($callback)) {
return $this->renderView($callback);
}
if (is_array($callback)) {
$callback[0] = new $callback[0]();
}
return call_user_func($callback, $this->request);
}
public function renderView($view, $params = [])
{
return Application::$app->view->renderView($view, $params);
}
}
Executable
+39
View File
@@ -0,0 +1,39 @@
<?php
namespace Core;
class View
{
public function renderView($view, $params = [])
{
$viewContent = $this->renderOnlyView($view, $params);
$layoutContent = $this->layoutContent();
return str_replace('{{content}}', $viewContent, $layoutContent);
}
protected function layoutContent()
{
ob_start();
include_once Application::$ROOT_DIR . "/app/Views/layouts/main.php";
return ob_get_clean();
}
protected function renderOnlyView($view, $params)
{
foreach ($params as $key => $value) {
$$key = $value;
}
ob_start();
include_once Application::$ROOT_DIR . "/app/Views/$view.php";
$content = ob_get_clean();
// Simple {{variable}} replacement
foreach ($params as $key => $value) {
if (is_scalar($value)) {
$content = str_replace('{{'.$key.'}}', $value, $content);
}
}
return $content;
}
}