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
+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);
}
}