I am doing tests with MVC in PHP, and I am having problems when it comes to routing to another controller, by default there is a controller and method (action) assigned in case of not finding what was received.
The problem is when including the driver in the routing, it says that it does not find the class in the current directory, the path is absolute.
I agree in this way so that the error occursdominio/articles
The only way that works for me is with dominio/home/algun_metodo
The .htaccess
RewriteEngine On
AddDefaultCharset utf-8
RewriteRule ^articles/([a-z_-]+)/?$ index.php?controller=articles&action=$1 [L]
RewriteRule ^([a-z_-]*)/?$ index.php?controller=$1&action=index [L]
RewriteRule ^([a-z_-]*)/?([a-z_-]*)/?$ index.php?controller=$1&action=$2 [L]
The index.php in the root of the project
// ONLY DEBUG
error_reporting(E_ALL);
ini_set('display_errors', 1);
// DEPENDENCIAS (FUNCIONAN PERFECTAMENTE)
require_once '_core_/_config.php';
require_once '_core_/_utils.php';
require_once '_core_/_model.php';
require_once '_core_/_controller.php';
// VISTA GENÉRICA (ESTE LLAMA AL ROUTING)
require_once '_core_/_view.php';
The _view.php
<!DOCTYPE html>
<html>
<head>
<!-- metas -->
<!-- links -->
</head
<body>
<?php require_once('_routes.php');?>
<!-- scripts -->
</body>
</html>
The _route.php here is where the fatal error occurs
function _loadController($controller)
{
$controller_path = PATH_CONTROLLERS.PREFIX_CONTROLLER.$controller.'.php';
if (!file_exists($controller_path)) {
$controller_path = PATH_CONTROLLERS.PREFIX_CONTROLLER.DEFAULT_CONTROLLER.'.php';
}
require_once $controller_path;
die('El require_once para cargar el controlador no funciona:
Fatal Error - Class not found en este fichero (está en este: /src/controllers/cnt_articles.php)
pero la ruta es absoluta, que falla?');
$controller_name = ucfirst(PREFIX_CONTROLLER).$controller;
return new $controller_name;
}
$controller = _loadController(isset($_GET['controller']) &&
!empty($_GET['controller'])
? $_GET['controller'] : DEFAULT_CONTROLLER);
$method = isset($_GET['action']) &&
!empty($_GET['action']) &&
method_exists($controller, $_GET['action'])
? $_GET['action'] : DEFAULT_METHOD;
$controller->{$method}();
Some constants of _config.php
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
define("PATH_MODELS",ROOT_PATH."/src/models/");
define("PATH_VIEWS",ROOT_PATH."/src/views/");
define("PATH_CONTROLLERS",ROOT_PATH."/src/controllers/");
define("PREFIX_MODEL","mdl_");
define("PREFIX_VIEW","vw_");
define("PREFIX_CONTROLLER","cnt_");
define("SUFIX_HELPER","_helper");