4

I am currently trying to understand how the MVC framework does work in PHP. Therefore, I have created this basic sketch of how I think that MVC is implemented in PHP:

Sketch of how MVC in PHP may be implemented

[I know that some steps are missing, e.g. how the Routerparses the route in order to know what View and Controller to load, but these steps are rather technical based and are not important in order to understand the general flow of MVC in PHP.]

I draw my understanding of MVC in PHP from this article series. However, I think that this structure will differ from most of the structures people think off, when they talk about MVC in PHP, because of this article (The article basically states that not only the Controller but also the View does communicate with the Model).

What I'd like to ask you now are several questions:

  1. Is this generally a right way of implementing MVC in PHP?
  2. How can I add a Login/Logout-System to this approach? So that user, who are not logged in, will always see the LoginView and users who are logged in can see different views? (I know how a login system does work, but I can't figure out where to place it in the MVC, so I don't have to insert the code multiple times, e.g. in each Controller.)
  3. What if my application consist of multiple elements (say a user bar [with user name, link to settings, etc.], a navigation and a content container) that are always loaded? How can I assemble these elements to a final view? (The only idea that comes to my mind is to assemble the final view in each view separately, but that would mean that I have to insert the code for it multiple times in each view, which misses the point of MVC, doesn't it?)
  4. What if I want to use AJAX in my application? My idea would be to send ajax requests through the framework as well, while only accessing controllers and views that are made for ajax? In other words the AjaxViews do only return e.g. json objects and the AjaxControllers do always expect authentication codes to prove that these ajax calls are legtitim?

I know that there have already been asked dozens of questions about MVC in PHP and I've been reading quiet a lot of articles until now, but I think that only reading does not enable me to understand MVC completely.

Furthermore, after reading the articles linked above, I'm not longer sure whether other articles about MVC, I find on the web, does explain MVC the same way as the articles above. Because if they don't, I'm trying to understand one framework while reading about two or multiple different approaches.

Thank you very much in advance for taking the time to answer my question!

-- --- --- Update --- --- --

According to the answer below I've changed my MVC sketch. Just in case someone finds this link and wants to know more.

enter image description here

Community
  • 1
  • 1
Robin
  • 1,208
  • 8
  • 17
  • What you are describing is not a MVC, but a rails clone of sorts. This is a dupe. There are tons of StackOverflow posts with users who think they learned what MVC is, but got tricked by bad marketing of popular framework that claim MVC but are not even remotely. For one the model is not a persistence layer to the db and the controller should not mediate between the model and view (and should not pass the data either). The view should react to changes in the model. http://stackoverflow.com/questions/18048960/is-this-the-right-way-to-structure-mvc-understanding-gathered-from-tereskos-po – Ronni Skansing May 10 '14 at 09:47
  • @RonniSkansing - I think I didn't refer to any marketing of a popular framework, did I? Please let me know. Furthermore, I thought that my controller is not mediating between the model and the view nor is he passing data from the model to the view. Did you interpret this from my sketch? Than my sketch is probably not precise and I need to improve it. As I am talking about MVC in PHP there is no observer structure, because the whole application is reloaded when an action is triggered (exception: AJAX),so that is why the view does not need to react on changes in the Model, does he? – Robin May 10 '14 at 09:56
  • quite right, you did not mention any frameworks. Also you drawing is quite nice and is misread it at first. Imo there really is no MVC in php and serverside languages, some people call it web mvc but really they are rails clones. Trying to stick the whole process into the letters mvc really does not fit into the complete process of clicking something in the browser, making it send a request, handling the request, refreshing the view after a stateless http and so on. – Ronni Skansing May 10 '14 at 10:00
  • 1
    The *Front Controller* can also be a pattern of it's own, so I would not put it into an MVC sketch w/o making it distinct: http://en.wikipedia.org/wiki/Front_Controller_pattern – hakre May 10 '14 at 10:13

2 Answers2

4

Let me first answer your questions, then place my take on it.

  1. There's no right way of writing MVC. There are so many flavors and variations, and that get even multiplied when talking about web MVC.
  2. About Logging in and Logging out. I think the most robust system would be a Role Based Access Control combined with an Access Control List, see How can I implement an Access Control List in my Web MVC application?.
  3. There are generally two approaches, either you have a 1:1 ratio between Controllers and Views, and then after the Controller is done, your bootstrap script calls the View with the same name (LoginController, LoginView), or your Controller returns the View name along with action and parameters, to be called by the bootstrapper. The view then picks a template, and that template can include other sub-templates (like the user bar, or the footer).
  4. In that case, your view needs to have an ability to select a different template based on the Accept: HTTP header (and send something like Accept: application/json in your AJAX requests). Your view then returns a JSON template instead of an HTML template.

What is wrong with your sketch?

Your model isn't just the gateway to your database, it's where all the logic happens. All the computation. See this yet another excellent answer that explains How should a model be structured in MVC?.

The idea of MVC is to simply separate your application into three layers: Input (controller), Logic (model) and Output (view). This is to extend on the usual way PHP works (here's a request, give me a response, all in the same page).

For that reason, implementation details may vary, the concept is what matters. "Web MVC" is merely the result of good OOP practices and some naming convention someone made up a few decades ago.

Community
  • 1
  • 1
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

It is for parallel development and code reuse ability. There is a separation of concern how your system works and how users work. This provides a solution of the problem. There is boundry now, MVC.

Rashid
  • 11
  • 4