Jump to content

Model-view-controller Architecture


Guest Stefán Örvar Sigmundsson
 Share

Recommended Posts

Guest Stefán Örvarr Sigmundsson

I'm supposed to design a very simple website using PHP and the philosophy of MVC for school but I seem unable to get my head around it. I've search the web but most of what I find is about some MVC frameworks (Java, C#, ASP.NET), articles about the design pattern that either contradict other articles or don't include any actual code samples (preferable in PHP). Now after hours of going from one link on Google to the next I've began thinking that maybe there isn't anything of use on the MVC. I've tried looking for books on Amazon about it but most of them, judging from the TOC, only mention briefly the MVC and aren't centred around it. On YouTube I found a few videos about MVC but none with PHP code samples.Here are my questions:1. I read somewhere that MVC-designed websites have a single point of entry. Does that mean, no matter what you write, you always get redirected to, let's say, index.php?2. If that is the case, you must have parameters in the URL to know "where" on the website the user is - since "where" doesn't mean a separate page requested but actually index.php requesting some content. Should, in this case, index.php consist of a huge switch statement to check if any of the possible parameters are set, and if they have valid values? And, in the case of some strange combination of parameters, try to figure out what to do?3. Some articles state that the Controller should interact with the Model and the View, but the latter two should never interact with each other. Other articles say the latter two should never interact "directly" with each others. And yet other articles show diagrams where everybody interacts with everybody. What is the true flow of the MVC?Trust me, there will be more questions once these have been answered. Cheers in advance.

Link to comment
Share on other sites

1. Don't really know about that one, but you always "start" in one place.2. Yeah, but it doesn't have to be a switch - for example, you could just include based on a querstring variable, e.g. include($_GET['page'] . ".php"); though with better security obviously.3. Model <-> Controller <-> View. I don't see why the view and model would ever need to directly interact with each other. Obviously, though, they still do indirectly interact with each other.

Link to comment
Share on other sites

Guest Stefán Örvarr Sigmundsson

1. Wether you can or cannot access other other pages than index.php directly changes everything about the design so it's kinda important to get straight.2. Doing it this way (include($_GET['page'] . ".php")) kinda indicates a single point of entry since you're just including the other pages within index.php.3. I don't know what the difference is betwen direct and indirect interaction since I don't really understand how they interact.

Link to comment
Share on other sites

The difference between "direct" and "indirect" interaction is if you're explicitly asking for something on another layer, of if you're telling another layer to choose a layer on its own.If you have PHP code in HTML files, you have the controller (PHP) directly interacting with the view (plain HTML). If you have PHP produce HTML code without the help of external files, you have the model (HTML produced from PHP) interacting directly with the controller (PHP). If you have a files that dictate how should a certain end HTML look (Views), a map as to when each view is used (Model), and a PHP file to execute the view according to the model (Controller), you have an MVC setup.

Link to comment
Share on other sites

1,2. Single point of entry isn't related to actual file, it refers to what the user sees (as far as I can tell - do you have a link to the document in question?). They all see the same thing to begin with. Whether they then access different files, or whether they always run the same file but with different results, is an implementation detail and something that MVC doesn't concern itself with. You can implement it how you like.3. The model is the actual data. The view is what the user sees. The controller tells the view about the data, and also gets data back from the view and modifies the view based on that. As an example, you write a blog system. The model (database in this case) hold the actual posts. The controller performs SELECT, INSERT and UPDATE queries on the model (database), formats them into arrays nicely, and tells the view what they are. The view has a form, but the action of the form is the controller.

Edited by Synook
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...