3

I've always had a hard time understanding the full concepts of MVC, MVP, MVVM, etc. I'm not even sure if I am following any of these models so I'm hoping some of you could shed some light. My application uses PHP, JS, HTML, CSS, AJAX, MySQL as its core.

VIEW

I know that HTML / CSS are obviously the View, but sometimes, I have PHP generate HTML elements. Also, sometimes I have JS manipulate HTML elements to modify its CSS. I mean, PHP is technically a server-side language right? So does the fact that PHP can generate "View" elements change its role in the programming pattern?

DEDICATED FILE THAT HANDLES AJAX REQUESTS

I have a single PHP file that handles all AJAX requests sent from JS. I pass a unique POST or GET parameter to distinguish what the AJAX request is for and the PHP file has a bunch of If statements to handle these.

$req = $_REQUEST['type'];
if($req == 'get_users'){
// do stuff
}

Most of the functions are database writes and reads. Do I have it right that this is the controller?

MODEL

I am assuming that the model is the set of functions and classes I call periodically?

SUMMARY

My main question is whether or not a programming pattern can apply to more than one programming language even though one is client-side and another is server-side. Am I even following a programming pattern? If I am not or am somewhat close to one, does it matter if I am not following a pattern exactly? I think that is what is holding me up and hoping you all could help me understand.

Freddie
  • 691
  • 2
  • 9
  • 23
  • Start using some framework (i recommend you Zend - lot of docs, help, tutorials, and the biggest community). You gonna understand how MVC works very quickly. – Peter Aug 31 '12 at 13:32
  • I recommend Code Igniter for it. – J. Bruni Aug 31 '12 at 13:33
  • Ye ... NO. None of mentioned frameworks actually implements MVC or MVC-inspired pattern. – tereško Aug 31 '12 at 14:07
  • http://codeigniter.com/user_guide/overview/mvc.html - I'm aware CI is not "pure MVC" as the documentation may sound, but anyway it helps to introduce some concepts, in practice. It is, at least, MVC-inspired. – J. Bruni Aug 31 '12 at 14:32

3 Answers3

3

I know that HTML / CSS are obviously the View, but sometimes, I have PHP generate HTML elements.

Views are actually supposed to be instances which encompass presentation logic. They acquire information from model layer, and based on this information decide what response to send to the user. The response can be just a HTTP header or it might be a HTML document, which view has assembled from multiple templates.

So does the fact that PHP can generate "View" elements change its role in the programming pattern?

This kinda touches on the subject that, you cannot implement classical MVC (as defined for smalltalk), because view should be observing model for changes. This is extremely hard to implement for web, quite impractical and does not scale .. at all. Instead for web we tend to use Model 2 MVC, MVP and MVVM patterns, which are inspired by the original MVC. Also there is HMVC pattern, which is actually unrelated to MVC, but tries to solve same problems.

All those MVC-inspired patterns differ in how they implement presentation layer, which is where view too. In Model2 MVC pattern you have an active view, that request the information from the model layer. In MVP and MVVM the view is passive and receives data via controller-like structure.

Most of the functions are database writes and reads. Do I have it right that this is the controller?

If I understood what you meant by this then: no, not really.

In all MVC-inspired patterns the controller or controller-like structure is mostly responsible for changing the state of model layer and view. In MVP and MVVM it also request data from model layer, and in MVVM it even manipulates it.

But the interaction with storage (which might or might not be SQL database) is buried deep in model layer. There is a longer post, that I wrote on the subject.

I am assuming that the model is the set of functions and classes I call periodically?

Not entirely true. See the link above.

Am I even following a programming pattern?

Without the code it is hard to tell, but I am quite confident, that you are not strictly following any MVC-inspired design patter. What you are doing is implementing some form of SoC, which is core principle of MVC. I would say that you are at the stage when PHP developers actually start researching OOP principles and pattern, for further improvement.

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150
2

Most of the functions are database writes and reads. Do I have it right that this is the controller?

If it is:

  1. Deciding what to do with a request based on URL, then it is fulfilling the role of a controller
  2. Interacting with a database, applying business logic and returning some data, then it is fulfilling the role of a model
  3. Using that data to generate HTML / JSON / XML / anything else sent to the client, then it is fulfilling the role of a view

If it is doing multiple of the above, then it is being a hodgepodge of different roles and you should consider separating out your concerns.

I am assuming that the model is the set of functions and classes I call periodically?

The model is the code that interacts with your data and enforces business logic.

My main question is whether or not a programming pattern can apply to more than one programming language even though one is client-side and another is server-side.

Yes.

You can have client side JavaScript that edits the DOM (View), decides what to do based on user input (Controller) and uses XMLHttpRequest to fetch data from the server (Model).

Then you can have PHP that generates JSON for that JS (View), devices what to do based on the data in the request from the JS (Controller) and interacts with a database to get the data for the response (Model).

Am I even following a programming pattern?

It sounds like you might be following the god anti-pattern, but you've only given a brief overview of what you are doing.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

I mean, PHP is technically a server-side language right?

Yes, right. (Although it can be used locally, it can even be compiled to generate a desktop app binary, et cetera.)

So does the fact that PHP can generate "View" elements change its role in the programming pattern?

No. The core "View" concept is its separation from other parts of the application. Ideally, you would not have any business logic, database queries, etc., in the "View" layer. For example: first, you do all the calculations and operations. As a last step, you transform your result into a "View" for output (as HTML, or XML, or JSON...) You don't have "echo" statements mixed up with the SQL queries.

Do I have it right that this is the controller?

In your description, we have two distinct things: one can be called "router" - it is where you determine which function / method to call based on the HTTP request.

The "controller", in this case, would be that functions, which on their turn call the database read/write functions and the view-build functions. The controller interact with the model, and deliver the output. It is the model that should interact with the database.

I am assuming that the model is the set of functions and classes I call periodically?

The model is commonly the group of functions which read and write into the database. This is the main real world usage. But this is not the definition of "model" - it is only it's most common form.

For example: if you have a virtual store, which sells products. You would then have a "Product" model, i.e., a class to handle the insertion of a new product into the database, the update of a product information, the calculation of its price, everything else you may want to do in a "product". These methods must work in any controller, any route, any view - this is one goal for the pattern separation/organization of responsibilities. You should be able to create a whole new front-end website using the same store model.

whether or not a programming pattern can apply to more than one programming language even though one is client-side and another is server-side.

Yes. We use lots of different languages in a single application: HTML, JavaScript, PHP, SQL, et cetera. The whole application can follow a set of patterns. Each language can be used to perform one or more roles in the pattern.

Am I even following a programming pattern? If I am not or am somewhat close to one, does it matter if I am not following a pattern exactly?

Yes, you are. I think that, even if you don't have a name for it, and even if you are following "your own pattern", you are certainly following some pattern, otherwise you would not be able to build your application. This pattern can be MVC, ABC, XYZ, CSM or whatever. Of course, if you learn the concepts, you are able to talk about your application code with other developers using these concepts. It surely is a good thing to learn, but...

No, it "doesn't matter" if you are not following a pattern exactly. You may end up creating a new, enhanced, better pattern! Sometimes being concerned in fit into some pattern may disrupt your creativity. Don't worry about it more than you should.

All this being said, I think you are doing right, wanting to know, to understand and even to master these patterns and their concepts. MVC is an important thing for a programmer to understand. A good teacher can help. Reading nice books and articles. Questions and answres here at SO. Some Google research... And, above all, coding experience. If you have a practical goal to reach, an app to code, and apply what you learn in practice, you will gradually understand them.

J. Bruni
  • 20,322
  • 12
  • 75
  • 92
  • CSM = Crazy Spaghetti Mess pattern – J. Bruni Aug 31 '12 at 13:55
  • you can also think about the "router" as a "front controller", i.e., a main and unique point-of-entry (index.php or ajax.php) which then, based in the HTP request, calls the multiple controllers available inside the app - the "products" controller, the "orders" controller, the "login" controller, and so on – J. Bruni Aug 31 '12 at 14:01