I’m trying to get my head around the Model-View-Presenter pattern and how it can be used in PHP websites.
When Googling MVC it seems most tutorials and frameworks claim to use MVC but actually have implemented MVP. Either way, I’ve looked into it and MVP looks like what I am trying to achieve in my code for better separation of concerns. This is likely to be a lengthy discussion if anyone has the stamina to answer my questions so I’ll only ask a few questions at a time.
Firstly, I understand MVP, to work like this:
- Presenter take user input and decides what information is needed; Presenter requests that data from Model.
- Model represents data though is not simply a class per database table (or JSON file, XML file, etc); it could be several classes. Presenter requests and manipulates data through public functions that represent tasks that model would carry out. For example a User model might have the public function getNumBlogsPosted().
- Once the Presenter has decided what to do it passes the data to the View. The View then loads a template file which is basically HTML with simple PHP or a templating language. Since I’m using PHP the View (presumably) escapes all the data for output (htmlspecialchars and urlencode).
Is this overview correct? Assuming it is, I have a few general questions:
- Is it right for the View to do the escaping or should that be done in the template?
- Would the Presenter do the URL routing or is that typically done before the Presenter is initialised?
- Similarly, where would you put hard-coded config like database connection details?
- Does the Presenter have to be a class? Are they any advantages to it being one? (See last point for why I ask this)
- Is a rule-of-thumb that the View is merely HTML generation, the Presenter takes user input, deals with the model and then sends it to the View and everything else should be done in the model. For example, should sending an email from a contact forms be done in the Model?
- Finally, my current bespoke sites have a bootstrap that loads settings and initialises about 15 core objects (like database connection object, formatter object, password hasher, etc) that are used by the front and back end.
Would I now need to inject all these as dependencies into the Presenter (seeing as I use Dependency Injection throughout)?
Thanks.