Cita:
.htaccessFatal error: Uncaught Error: Call to undefined method Exception::getMenssage() in C:\xampp\htdocs\mvc\public\index.php:15 Stack trace: #0 {main} thrown in C:\xampp\htdocs\mvc\public\index.php on line 15
Código:
index.phpRewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.+) index.php?url=$1 [QSA,L]
Código PHP:
<?php
chdir(dirname(__DIR__));
define("SYS_PATH","lib/");
define("APP_PATH","app/");
require SYS_PATH."Router.php";
require APP_PATH."http/routes.php";
try {
$action = Router::getAction($_GET["url"]);
$controllerName = $action["controller"];
$method = $action["method"];
require APP_PATH."controllers/".$controllerName.".php";
$controller = new $controllerName();
$controller->$method();
} catch (Exception $e) {
echo $e->getMenssage();
}
?>
Código PHP:
<?php
class Router{
private static $routes = [];
private function __construct(){}
public static function add($route,$controller,$method){
static::$routes[$route] = ["controller" => $controller, "method" => $method];
}
public static function getAction($route){
if(array_key_exists($route, static::$routes)){
return static::$routes[$route];
}else{
throw new Exception("The route '$route' was not found");
}
}
}
?>
Código PHP:
<?php
Router::add("home","MainController","index");
Router::add("about","MainController","about");
?>
Código PHP:
<?php
class MainController{
public function index(){
echo "Hola desde el index";
}
public function about(){
echo "Hola desde el about";
}
}
?>