Let me summarize the three parts of MVC again and map them to your directory structure and some general examples
The Model holds the state of your applicaton that should be displayed (or manipulated) by the current user request. In most cases this are some database objects which represent real entities: a user object which was just registered, product A which the user U wants to see (here A and U would both be part of the state) or the transaction data of a finished purchase (e.g., the user, the product, the credit card clearance information etc.). But it's not restricted to database objects, it could be, as you pointed out, an array of calculation results.
From a programming point of view this would translate in separate classes - probably also stored in a separate folder - which are only responsible for holding the state data and provide simple operations to manipulate it. A simple Example would be a Product
class which can be filled with data from the database and performs price/tax/discount operation on the product it represents (even though it could be argued the calculation should be in a separate class)
In your example this would probably some of the classes in your classes directory.
The Controller is the main logic that handles the user requests which is creating or retrieving the Model data (e.g., through a database request or performing a calculation) and forwarding it to the View. This part of the code is also often referred to as business logic. The controller of a web application usually looks like this: Check authorization of user requests to access this page, load required objects from database, perform operation on objects, move objects to view, clean up.
This would translate into the PHP page which is invoked by the user which is then forwarding the data to a separate template engine (the view) or another PHP page for display. In Java this would be a Servlet (or a framework component like a Struts Action).
In your example, this might be the index.php
, but if your application is a little bit more complex, you probably want to have a separate PHP page for each action your users want to do, like add_comment.php
, show_product.php
etc.
The View is the logic that will then display the state data which was passed along by the Controller. In a "pure" MVC world, this should not contain any business logic, do not perform any computation etc. but simply take the data and put it into certain areas of the UI which the user will see.
Again, for PHP this could be a template page (in Smarty) or a separate PHP file which only contains echo
statements and HTML, in Java this are mostly JSP pages etc. All the JavaScript, CSS, images and other files which are used to create the final page that is sent back to the user are also considered part of the view.
In your example, this would obviously be the css and js directory, but you probably mixed the view and business logic in your classes and/or index.php
, I guess.
The whole idea of MVC is separation of concerns, so that the code is reusable (the model data can be used by many actions) and easy to maintain, as you don't need to touch the controller if you want to change some colors and layout in the view.
So it doesn't really matter how you create your directories or files, if your application is MVC based depends on how you structure your code.