The requested resource / was not found on this server error with PHP routing

  Kiến thức lập trình

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

Pedro Neto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT