Hello! It turns out that the styles are not loaded when I am loading another view, I am new to mvc and I do not understand the error of this, but when I see the address it does not indicate me directly to the correct route but in the index (main front controller ) No problem :
<link rel="stylesheet" href="<?php base_url ?>assets/css/styles.css">
I use this way to refer to my address for the styles, my parameters are like this:
define("base_url", "http://localhost/mvc-crud/");
define("controller_default", "usuarioController");
define("action_default", "index");
In my front controller that I have by default to load the productController controller, it loads me the index method in which it has a require_once of the view, This loads the address without problem, but when trying with other pages it does not let me, if not indicates the following path:
http://localhost/mvc-crud/usuario/assets/css/styles.css
Here I get an error because it first loads the "user", which refers to userController, in my front controller I have it normal when loading the view:
http://localhost/mvc-crud/assets/css/styles.css
Image of my main view(no errors) :
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MVC CRUD</title>
<link rel="stylesheet" href="<?php base_url ?>assets/css/styles.css">
</head>
<body>
<!--FORM-->
<div class="container">
<div class="left"></div>
<div class="right">
<form action="<?php base_url ?>usuario/login" method="POST">
<h2>Inicio de Sesion</h2>
<div class="item">
<label for="nombre">Correo:</label>
<input type="text" name="nombre">
</div>
<div class="item">
<label for="nombre">password:</label>
<input type="password" name="password">
</div>
<input type="submit" value="Entrar">
<?php if (isset($_SESSION["error_login"])) : ?>
<p><?php echo $_SESSION["error_login"] ?></p>
<?php endif; ?>
<?php Utils::deleteSesion("error_login"); ?>
</form>
<a href="<?php base_url ?>usuario/register">Registate aquí!</a>
</div>
</div>
</body>
</html>
And the view that causes me problems like the others since the styles don't load:
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MVC CRUDD</title>
<link rel="stylesheet" href="<?php base_url ?>assets/css/styles.css">
</head>
<body>
<!--FORM-->
<div class="container">
<div class="left"></div>
<div class="right">
<form action="<?php base_url ?>usuario/register" method="POST">
<h2>Registro de usuario</h2>
<div class="item">
<label for="nombre">Nombre:</label>
<input type="text" name="nombre">
</div>
<div class="item">
<label for="nombre">Apellidos:</label>
<input type="password" name="password">
</div>
<div class="item">
<label for="nombre">Direccion:</label>
<input type="text" name="nombre">
</div>
<div class="item">
<label for="nombre">ciudad:</label>
<input type="password" name="password">
</div>
<div class="item">
<label for="nombre">telefono:</label>
<input type="text" name="nombre">
</div>
<div class="item">
<label for="nombre">correo:</label>
<input type="password" name="password">
</div>
<div class="item">
<label for="nombre">password:</label>
<input type="password" name="password">
</div>
<input type="submit" value="Registrarse">
<?php if (isset($_SESSION["error_login"])) : ?>
<p><?php echo $_SESSION["error_login"] ?></p>
<?php endif; ?>
<?php Utils::deleteSesion("error_login"); ?>
</form>
<a href="<?php base_url ?>">Volver!</a>
</div>
</div>
</body>
</html>
Thank you very much in advance! If I'm not clear, I'll be attentive to the comments!
In case more info is needed (front controller):
<?php
session_start();
require_once "autoload.php";
require_once "config/db.php";
require_once "config/parameters.php";
require_once "helpers/utils.php";
function showError()
{
$error = new errorController();
$error->index();
}
if (isset($_GET["controller"])) {
$nombre_controlador = $_GET["controller"] . "Controller";
} else if (!isset($_GET["controller"]) && !isset($_GET["action"])) {
$nombre_controlador = controller_default;
} else {
showError();
die();
}
if (class_exists($nombre_controlador)) {
$controlador = new $nombre_controlador();
if (isset($_GET["action"]) && method_exists($controlador, $_GET["action"])) {
$action = $_GET["action"];
$controlador->$action();
} else if (!isset($_GET["controller"]) && !isset($_GET["action"])) {
$action = action_default;
$controlador->$action();
} else {
showError();
}
} else {
showError();
}
edit:
define("base_url", "http://localhost/mvc-crud/");
define("controller_default", "usuarioController");
define("action_default", "index");
The content of my .htaccess :
<IfModule mod_rewrite.c>
# Activar rewrite
RewriteEngine on
ErrorDocument 404 http://localhost/mvc-crud/Error/index
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)/(.*) index.php?controller=$1&action=$2
</IfModule>
Image of how the folders are organized:
I see that you do the rewrite but you don't put the corresponding exceptions so the htaccess tries to look for the styles inside the rewrite.
This is my file and it works for me, if the previous line does not work for you, try this: