SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to implement MVC?

    I know MVC is Model View Controller. I know their roles. But I don't know how to name the files and put them into different folders.

    What if I'm building this class that handles everything for with regards to shipping rates.

    Class name => shippingRate
    functions to perform
    => getRateFromDatabase
    => addRate
    => editRate
    => deleteRate
    => calculateRate
    => displayRate
    Something like that.
    Maybe I have to break them into different classes? How would you name them then?
    How would you name the files and link all the classes together?

    Basically, I would like to see a working example of how to implement MVC.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by resting
    Basically, I would like to see a working example of how to implement MVC.
    Pick up a framework like CodeIgniter or Symfony which implement the web MVC pattern. Walk through one of the code tutorials, the "how to build a blog" type stuff. You'll get how it works after that.

    You have models, views and controllers. They are separate. The code to format and display the rates will be in a view class or template. The code to represent the rates in the database will be in a model class. The code that says "hey a user has clicked the checkout button, let's compute shipping rates and display them", then builds a model object and passes it to the view for rendering, is the controller.

  3. #3
    SitePoint Addict aguroyz's Avatar
    Join Date
    Jan 2009
    Location
    Konoha Fire Country
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe I have to break them into different classes? How would you name them then?
    Sometimes I tend to separate them for me to easily read my codes..
    Like I separate addrate, editrate to different controller classes..

    and I think separating them will make the class load faster since you separated the methods according to function...

    But I'm not quite sure if my idea's right though
    Uniquely FILIPINO... See how talented and creative Filipinos are.
    http://www.smalltym.com
    Custom Web Designs:
    http://proweaver.com

  4. #4
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a full scale framework is too overkill for my small project.

    i just wanted to make use of the logic of MVC to break my classes to manageable parts.

    but then, i realised a class of shippingRate will contain

    1. retrieving of information from the database (model)
    2. processing of the retrieved information (control)
    3. echoing of the result in HTML (view).

    where then, should i place shippingRate class?
    or if I were to break them up, I'll end up with 3 classes instead of 1.

    problems:
    1. I have to think of 3 names instead of 1.
    2. Should I name the file name according to the class name and place them in the correct folder?
    3. Do I include all 3 files in the file that needs to display the shipping rate?

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I suggested you walk through a tutorial to understand MVC, not that you use a framework for your project.

    It sounds like you really don't want to use the MVC pattern. You don't have to, nobody is forcing you to. If it makes more sense to you to use one class for everything related to shipping rates in your project, then do that. Patterns are just common solutions to problems that you might want to use to save time, they are not something you must rigidly conform to.


Bookmarks

Posting Permissions

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