/    Sign up×
Community /Pin to ProfileBookmark

Question about MVC

I, more or less, “get” MVC when it comes to generic explanations but I’m having trouble applying it to a script I’d actually make.

Okay, picture a browser-based virtual dog game. You know, HTML, jpeg images, CSS, and perhaps a little JavaScript. Each dog would have its own profile page. If it’s your dog, then you could change its details. So, its page would have forms on it, such as a form to change its name. You’d submit the form and some code in the site would process it (check for errors, update the database, etc.)

Would the HTML for the form be considered part of the view or part of the controller?

What about the code that actually processes the code, is that the controller?

Finally, the code that actually updates the database, that’s the model, right?

Please note: What if the form automatically had the dog’s name already in it. For example, if your dog’s name was “Fido” the form to change its name would automatically have “Fido” already typed in it. Is this part of the view? Or is it part of the model because the site had to look in the database to know that’s your dog’s name?

I’m confident I’ll wrap my head around all this eventually ?

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@Jeff_MottJul 19.2012 — Would the HTML for the form be considered part of the view or part of the controller?[/QUOTE]

The view.

What about the code that actually processes the code, is that the controller?[/quote]

Yep.

Finally, the code that actually updates the database, that's the model, right?[/QUOTE]

Also yep.

Please note: What if the form automatically had the dog's name already in it. For example, if your dog's name was "Fido" the form to change its name would automatically have "Fido" already typed in it. Is this part of the view? Or is it part of the model because the site had to look in the database to know that's your dog's name?[/QUOTE]

This one requires a combination. The controller acts as a mediator between the database and the templates. The controller would query the database for the dog data, then the controller would pass that data to the templates for rendering.

I'm confident I'll wrap my head around all this eventually ?[/QUOTE]

You seem to be doing well so far. ?

You may also find this article useful: http://symfony.com/doc/current/book/from_flat_php_to_symfony2.html

You can stop reading two-thirds of the way through when it starts including Symfony components if you're not interested in learning Symfony, but the first two-thirds of that article take you step-by-step in applying MVC to a flat PHP script.
Copy linkTweet thisAlerts:
@NogDogJul 19.2012 — Can't really add anything meaningful beyond what Jeff said. However, if you've not checked it out already, you might take a look at the CodeIgniter tutorial videos. In particular, the 2nd (and longer) video shows some "real" usage for a very basic blogging system. If you're not interested in using CI, you don't need to pay close attention to syntax and such, just how things get allocated and exchanged between the M, V, and C.
Copy linkTweet thisAlerts:
@evenstar7139authorJul 21.2012 — If I'm using OOP, is each controller a class? Like, if I had a virtual dog game, would I have a dog class/controller, a user class/controller, etc.?

And is my model just a database class?

The view would not have any classes in it, right? The only PHP code would be that needed to actually present data. Yes?

Also, the files visitors actually visit (e.g. http://www.mydomainname.com/userprofile.php) are those separate from the view?
Copy linkTweet thisAlerts:
@NogDogJul 22.2012 — The model essentially manipulates your data. This usually includes database access, but it can also include data calculations (such as business rules or, in your case, game rules). However, I see many people [i]only[/i] use the models for database access and end up putting a lot of their data manipulation in the controllers.

The controller is the traffic cop, deciding what to do when a request comes in, which model(s) to invoke, what to send to/read from those models, which view to invoke, and what data to send to the view.

Generally, your controllers and models will always be objects -- especially if you're using a framework that depends on your controllers and models to extend base classes. Within your controller classes, you might have multiple methods which represent different specific types of requests within a general request type. In CI, for example, if your url calls "www.example.com/foo/bar/", it will invoke the bar() method within the Foo class (after invoking the constructor method if there is one).

The view [I]could[/I] be a class, but I don't think I've seen anyone actually do that. It's usually either procedural PHP or a templating system (such as Smarty). You never invoke (or redirect to) the view directly via HTTP -- you always (normally) invoke a controller, which will then invoke the applicable view (internally probably via include() or require()), which allows the controller to make any output variables/objects available for use within the view.

So, after all that rambling, chances are Dog and User would be models: things whose data you want to track and manipulate. Your controllers might include an Action controller, which could include a feedDog() method, a walkDog() method, and so forth. Both methods might invoke the same view, but with somewhat different data that would control what sort of message is displayed to the user, while also displaying the current user and dog stats (common to any action).
Copy linkTweet thisAlerts:
@evenstar7139authorJul 22.2012 — So, does this mean, a site that uses MVC wouldn't have URLs that look like this?: http://www.somesite.com/login.php

It's URLs would look more like this?:

http://www.somesite.com/user/login/

Also wouldn't FeedDog() and WalkDog() be models if the criteria for a model is to manipulate data? FeedDog() for instance. A dog's fullness level would be stored in the database. Maybe the number 5 would represent its belly completely full and 0 would represent it completely empty. To feed a dog, this number would have to be changed.
Copy linkTweet thisAlerts:
@NogDogJul 22.2012 — It does not have to, but it is a common technique. Typically, you can also pass additional parameters that way, e.g "http://example.com/action/walk_dog/1.5" might be rewritten by the web server so that it is equivalent to "http://example.com/controller/action.php?action=walk_dog&miles=1.5". But that is just (?) an implementation detail -- it is not what makes an application MVC or not MVC.
Copy linkTweet thisAlerts:
@evenstar7139authorJul 22.2012 — I edited my last reply I think while you were responding. I don't think you saw the last paragraph. Could you take a look?

LOL I'm so overwhelmed with this MVC thing. I'm slowly grasping it, but it's taking awhile. I'll know I understand it when I can take one of my old scripts and break it up into model-related tasks, controller-related tasks, and view-related tasks.

Reading written tutorials, watching tutorial videos, and talking to you guys is slowly removing the fog.
Copy linkTweet thisAlerts:
@George88Jul 22.2012 — This video is a little old, but it's very easy to follow.

It's a walk-through tutorial for building a very basic, very simple MVC framework.

http://www.youtube.com/watch?v=CGiIVQPaOJQ
Copy linkTweet thisAlerts:
@NogDogJul 22.2012 — In my example, feedDog() and walkDog() are different actions that are initially represented by methods within a controller. However, that does not mean there could not be similar actions within the models (similar in terms of general subject, not in terms of implementation details): you may need a Dog::feed() method for you Dog model to implement the effects, and method within User or some other model (Inventory?) to record the expenditure of dog food resources.

So, your framework would initially invoke your Action controller's feedDog() method, which then itself would invoke model methods Dog::feed() and Inventory::feedDog() (or something like that). Then when all that processing is done, the controller finally would invoke your "status" view (for example).
Copy linkTweet thisAlerts:
@evenstar7139authorJul 23.2012 — This video is a little old, but it's very easy to follow.

It's a walk-through tutorial for building a very basic, very simple MVC framework.

http://www.youtube.com/watch?v=CGiIVQPaOJQ[/QUOTE]


Wow, it makes a lot more sense after watching that.
Copy linkTweet thisAlerts:
@evenstar7139authorJul 24.2012 — Okay, so, I think I'm almost ready to write a simple MVC framework on my own. The video George88 linked to helped a lot in understanding how MVC works but I can't design my MVC like the one in the video because its outdated. Where can I go to learn how to make an up-to-date MVC? I've found a ton of tutorials on Google but I don't know which one to trust. I want to learn this the "right" way.

Edit: This is one tutorial I was considering: http://www.youtube.com/watch?v=Aw28-krO7ZM
×

Success!

Help @evenstar7139 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...