fixing stuff
This commit is contained in:
@@ -1,2 +1,4 @@
|
||||
<?php
|
||||
$router->get('/', '/', [new HomeController(), 'home']);
|
||||
use src\Controller\HomeController;
|
||||
|
||||
$router->get('/', [new HomeController(), 'index']);
|
||||
@@ -1,6 +1,8 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../src/Autoloader.php';
|
||||
|
||||
use src\Router;
|
||||
|
||||
$autoloader = new Autoloader();
|
||||
$autoloader->addNamespace('src', __DIR__ . '/../src');
|
||||
$autoloader->register();
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
<?php
|
||||
namespace src\Controller;
|
||||
|
||||
abstract class BaseController
|
||||
{
|
||||
protected function render(string $view, array $data = []): void
|
||||
{
|
||||
extract($data);
|
||||
|
||||
$viewPath = __DIR__ . '/../../views/' . $view . '.php';
|
||||
$viewPath = __DIR__ . '/../../templates/' . $view . '.php';
|
||||
|
||||
if (!file_exists($viewPath)) {
|
||||
http_response_code(500);
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<?php
|
||||
namespace src\Controller;
|
||||
|
||||
class HomeController extends BaseController
|
||||
{
|
||||
public function index(): void
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
<?php
|
||||
namespace src
|
||||
namespace src;
|
||||
|
||||
public class Router
|
||||
class Router
|
||||
{
|
||||
private array $routes = [];
|
||||
|
||||
public function get(string $path, cllable $handler): void
|
||||
public function get(string $path, callable $handler): void
|
||||
{
|
||||
$this->routes['GET'][$path] = $handler;
|
||||
}
|
||||
@@ -17,7 +17,8 @@ public class Router
|
||||
|
||||
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) {
|
||||
http_response_code(404);
|
||||
|
||||
Reference in New Issue
Block a user