lundi 12 octobre 2015

Abstracting form submission in the controller

I'm using Laravel for a project that provides student progress reports for educational institutions to complete.

As for loading the correct report form for a student, every student is on a course, represented by "course_id" on the "courses" table which maps to a value: phd, master, bachelor etc and I load the respective view for the student based on the course they belong to. The student is determined by a parameter in the request URL.

The issue I have is with submitting the form, as I'm trying to abstract the process in my code but unsure where to begin. In a previous version I had a controller for each course: PhdController, MasterController, etc. each with their own method for the processing the form data i.e. validating, uploading files adding all data to DB, sending response to user after submitting. I feel this approach is wrong and violating the DRY principle, so want to remedy this.

I'm wondering if it's possible to have a single ReportController (as opposed to one per course) that would take some sort of report interface/object that would call it's own submit method when the form is submitted. Each course would require an implementation of this interface/object. For example let's say we had a PhD student form loaded up and someone submitted the form this would go to the ReportController's submit method which would then refer back to the PhD implementation of this interface/object and call it's submit method which would do validation via a FormRequest object then if passed it would upload any files then submit the data to the database. What I'm especially struggling with is how the type of object is determined at run time.

Any guidance on this would be highly appreciated! Thank you.

Aucun commentaire:

Enregistrer un commentaire