fixing stuff

This commit is contained in:
2026-04-21 14:03:37 +02:00
parent 3de979fc65
commit 7ee3d6441a
5 changed files with 16 additions and 7 deletions

View File

@@ -1,2 +1,4 @@
<?php <?php
$router->get('/', '/', [new HomeController(), 'home']); use src\Controller\HomeController;
$router->get('/', [new HomeController(), 'index']);

View File

@@ -1,6 +1,8 @@
<?php <?php
require_once __DIR__ . '/../src/Autoloader.php'; require_once __DIR__ . '/../src/Autoloader.php';
use src\Router;
$autoloader = new Autoloader(); $autoloader = new Autoloader();
$autoloader->addNamespace('src', __DIR__ . '/../src'); $autoloader->addNamespace('src', __DIR__ . '/../src');
$autoloader->register(); $autoloader->register();

View File

@@ -1,11 +1,13 @@
<?php <?php
namespace src\Controller;
abstract class BaseController abstract class BaseController
{ {
protected function render(string $view, array $data = []): void protected function render(string $view, array $data = []): void
{ {
extract($data); extract($data);
$viewPath = __DIR__ . '/../../views/' . $view . '.php'; $viewPath = __DIR__ . '/../../templates/' . $view . '.php';
if (!file_exists($viewPath)) { if (!file_exists($viewPath)) {
http_response_code(500); http_response_code(500);

View File

@@ -1,4 +1,6 @@
<?php <?php
namespace src\Controller;
class HomeController extends BaseController class HomeController extends BaseController
{ {
public function index(): void public function index(): void

View File

@@ -1,11 +1,11 @@
<?php <?php
namespace src namespace src;
public class Router class Router
{ {
private array $routes = []; private array $routes = [];
public function get(string $path, cllable $handler): void public function get(string $path, callable $handler): void
{ {
$this->routes['GET'][$path] = $handler; $this->routes['GET'][$path] = $handler;
} }
@@ -17,7 +17,8 @@ public class Router
public function dispatch(string $method, string $uri): void public function dispatch(string $method, string $uri): void
{ {
$handler = $this->routes[$method][$uri] ?? null; $path = parse_url($uri, PHP_URL_PATH) ?? '/';
$handler = $this->routes[$method][$path] ?? null;
if ($handler === null) { if ($handler === null) {
http_response_code(404); http_response_code(404);