Recently, I wanted to create my own framework using the MVC pattern to test my skills. (Clarification, it is developed in php and mysql is used for BD) My framework has the following architecture:
In summary:
The problem I have is with the exceptions. What I did was (although I do not think it's okay) to use a try / catch in the index (file that starts the execution of my application) to capture any exception that is released later (that is, in controller.php, modelo.php , connection.php, vista.php, etc) Simplifying details, it would be something like this:
index.php
$ruta = new controladorDeRutas();
try{
$ruta->comenzar();
}
catch(Excepcion404 $e){
Vista::crearVista("nombreDeLaVistaAMostrar", $e);
}
catch(ExcepcionConexionABaseDeDatos $e){
Vista::crearVista("nombreDeLaVistaAMostrar", $e);
}
...etc
That's why if an error is thrown in one of the files, I capture it in the initiator of my process (index.php). The issue is that I do not know if this is all right. It had occurred to me, on the other hand, to throw exceptions and capture them in the other files, instead of concentrating everything on the index. Example:
conexion.php
...código...
if($conexion->errno){
throw new ExcepcionConexionABaseDeDatos;
}
...código...
modelo.php
...código...
public function buscarTodosLosUsuarios(){
$conexion = new Conexion();
...código...
}
...código...
controller.php
public function mostrarUsuarios(){
$modelo = new Modelo();
try{
$modelo->buscarTodosLosUsuarios();
Vista::crearVista("nombreDeLaVistaAMostrar");
}catch($e){
Vista::crearVista("nombreDeLaVistaAMostrar", $e);
}
}
I do not know if it was understood. In the first case I capture all the exceptions in the index with a single try / catch. In the second case you should try / catch for each exception that occurs, to capture it in the controller and show the corresponding view. In short (summary):
- Should it capture all the exceptions thrown in the index?
- Is it appropriate to capture the exceptions in each particular case from the controller?
- Which option would you choose? Can you think of any other idea?
If you read this far, thank you for taking the trouble to do so. Thank you very much.