I’m trying to create a PHP project using MVC and routing, but I think my routes are not being appropriately understood by the server. This is my routes.php:
<?php
function load(string $controller, string $action){
try{
$controllerNamespace = "app\controllers\{$controller}";
if(!class_exists($controllerNamespace)){
throw new Exception("O controlador {$controller} não existe!");
}
$controllerInstance = new $controllerNamespace();
if(!method_exists($controllerInstance, $action)){
throw new Exception("O método {$action} não existe no controlador {$controller}!");
}
$controllerInstance->$action((object)$_REQUEST);
} catch(Exception $e){
echo $e->getMessage();
};
}
$router = [
'GET'=> [
'/' => fn() => load('HomeController','index'),
'/contact' => fn() => load('ContactController','index')
],
'POST'=> [
'/contact' => fn() => load('ContactController','store')
]
];
?>
This is the repository, if anyone wants to see the project structure: https://github.com/pedronet00/teste_roteamento
I’ve tried to use the XAMPP server, but it did not work out as well. I’m using php -S localhost:3000 to run the project.
New contributor