I am after some advice on a PHP situation.
Currently I have an HTML page that has two username and password fields. When a ‘submit’ button is pressed, a PHP file is loaded via the Action and if the username and password are correct, a game starts up. The game is in the same PHP file as the checking for the user. The user can answer many questions and gets scored based on their results. After the user has finished, they can press a logout button that saves their current score and level.
I have been asked to use a model/view/controller setup. Can I please have some advice on the process that I should undergo to modify my one PHP game file into this sort of setup?
I’m excluding the functioning of the game itself, and using your authentication as an example.
Controller
The controller in your case is some page that receives the form submission. It will validate any inputs, and decide what service to call to get a response. It will then pass that response (which should include only data that is going to be displayed on the page, and nothing regarding formatting or other information) to a view.
Model
The model in this case is the service that provides the authentication. This should be a set of functions, such as:
Usually these are encapsulated in a class that can be called from anywhere. This is your ‘model’ because it interacts with the data of the problem’s domain.
View
In this case, once the controller passes the response from the authentication service to the view, the view will unpack it and mark it up with html and css. In this case it will probably mean showing the user’s login name and grabbing the information for the game. Or, if the login failed, the view should show be all about the failure; the error message, etc. Note these are two different views: success and failure of authentication.