3
When we are developing a system using MVC from scratch, each controller needs to be a class or I can simply use a file containing some functions that will be called as the action sent by View?
Example, I have a controller for the "profile" view that displays the user’s data and updates them if necessary.
include_once('model/usuario.php');
class Controller{
public $dadosUser;
function __construct($action = null){
if(empty($action)){
$user = new Usuario();
$id = $user->id;
$this->dadosUser = $user->loadUsr($id);
}
}
}
In this case, you can change the value of $action when a new Controller is instantiated?
Another example is a generic file that I am implementing as follows:
include_once('../model/usuario.php');
$action = isset($_POST['action']) ? $_POST['action'] : "";
if($action){
switch($action){
case 'login':login(); break;
case 'listar':getAllAqr();break;
default: break;
}
}
function login(){
$email = isset($_POST['email']) ? $_POST['email'] : "";
$pwd = isset($_POST['senha']) ? $_POST['senha'] : "";
$usr = new Usuario();
$usr->login($email, $pwd);
$id = $usr->id;
if($id){
header("Location: http://localhost/aldeia/dashboard.php");
}else
header("Location: http://localhost/aldeia/login.php");
}
Which of these two forms is the most suitable?