I'm a beginner in PHP and Design Patterns. I'm having trouble implementing use case controller in an application containing javascript and php. I'm trying to make a project of student profile manager. I have a use case of "Upload Marks" in which teacher uploads students' marks of a particular section. And I have assigned the responsibility of system operations in this use case to a class named UploadMarksEventHandler. I have only mentioned the methods and attributes and they are not supposed to produce an output yet. Here is the code:
class UploadMarksHandler{
private $section;
private $central_storage;
private $students = array();
private $cookie_name;
private isSelectSection = false;
function __construct(){
}
function login($teacher_id, $password){
$password_temp=$this.centr/al_storage.getTeacherPassword($teacher_id);
if($password_temp==$password){
//set cookies
setcookie($teacher_id, $password, time()+(60), "/");
}
else{
echo "incorrect password";
}
}
function selectSection($section){
//check cookies
$this->section=$this->central_storage.getSection($section);
$this->is_section_selected=true;
$this->students=$this->central_storage.getStudents($this->section);
}
function uploadMarks($student_id, $marks){
//check cookies
}
function display_students(){
//check cookies
//display $this->students
}
function endUseCase(){
//logout
//destroy central storage object
//destroy section object
//destroy students object
//destroy object
}
}
?>
The above code is for the application logic layer. The user from the UI layer will perform operations such as login($teacher_id, $password), uploadMarks($student_id, $marks), e.t.c. And each of these operations are given as responsibilities to the controller class UploadMarksHandler. The problem is, the user will generate operations from the UI layer, which I'm planning to code in jquery. Suppose the user presses a button on the browser, named "Upload Marks". For this I will use AJAX to create an UploadMarksHandler object. I will do it in a file main.php which will be used for the purpose of mapping the UI layer onto the application logic layer.
$upload_marks = new UploadMarksHandler(); //this is in main.php, which is called through AJAX
After instantiating the $upload_marks object which is responsible for handling the "Upload Marks" use case, my application should listen to the next system operation in the UI layer. Suppose the user presses a button named "Login". Now when the control moves back to the server-side from the client-side, my $upload_marks object would be deleted and hence its state will be lost.
I have searched about applying MVC and controller in PHP but found no luck regarding my problem. Should I use a MVC framework? Any help is appreciated.
Suppose the user presses a button named "Login". Now when the control moves back to the server-side from the client-side, my $upload_marks object would be deleted and hence its state will be lost.
When the user triggers the UploadMarks event, create an object that tracks the state of that workflow. At the end of a request, persist the current state of the workflow into the database. Whenever the user clicks on UploadMarks again, fetch the state and resume the workflow.
I have searched about applying MVC and controller in PHP but found no luck regarding my problem. Should I use a MVC framework?
The best advice I can give you about MVC is not to worry about it. MVC has gotten a lot of undue attention over the recent years. It got hyped like it was some holy grail when it's really just a tiny detail in your app.