www.webdeveloper.com
Results 1 to 12 of 12

Thread: Question about MVC

  1. #1
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357

    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
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  2. #2
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Quote Originally Posted by evenstar7139 View Post
    Would the HTML for the form be considered part of the view or part of the controller?
    The view.

    Quote Originally Posted by evenstar7139 View Post
    What about the code that actually processes the code, is that the controller?
    Yep.

    Quote Originally Posted by evenstar7139 View Post
    Finally, the code that actually updates the database, that's the model, right?
    Also yep.

    Quote Originally Posted by evenstar7139 View Post
    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?
    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.

    Quote Originally Posted by evenstar7139 View Post
    I'm confident I'll wrap my head around all this eventually
    You seem to be doing well so far.

    You may also find this article useful: http://symfony.com/doc/current/book/..._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.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    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?
    Last edited by evenstar7139; 07-21-2012 at 03:09 PM.
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    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 only 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 could 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).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    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.
    Last edited by evenstar7139; 07-21-2012 at 08:06 PM.
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    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.
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  9. #9
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    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

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    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).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  11. #11
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    Quote Originally Posted by George88 View Post
    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
    Wow, it makes a lot more sense after watching that.
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  12. #12
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    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
    Last edited by evenstar7139; 07-23-2012 at 09:36 PM.
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles