Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 20

Thread: OOP and MVC

  1. #1
    CPD
    CPD is offline
    Member
    Join Date
    May 2012
    Location
    South London, England
    Posts
    36
    Member #
    31775
    Liked
    5 times
    I'm new to these forums and thought I'd kick start my life here with a topic that's been bouncing around in the back of my head for a while now.

    I'm fond of the MVC pattern and its use in PHP but I need to clarify my understanding before I continually use the method; possibly in an incorrect fashion.

    So far I've been implementing my web applications with a URI such as domain.com/controller/action/?queryString with the use of some tasty htaccess.

    The controller has a method for each individual page and the page called would be the action; there are many controllers each of which relate to various "areas" within the application. Moreover, I have an intermediate step which process any forms submitted by the end user before processing the action.

    The action inside the controller ultimately sets variables within a Template engine and retrieves any data it requires from the database through its own model dedicated model or other models if they are required (E.g. one controller may need authentication before it can be used therefore it calls upon a SessionModel to authenticate the user).

    I just want to confirm my understanding of the controller handling requests, the model retrieving data and manipulating data at the lowest level and giving it back to the controller, and finally the Template engine formatting the template its given with the variables its given. The controller then displays the template.

    I understand this seems as a "brief" explanation for such a vast topic but its with the intent of being open ended in order to acquire varied responses and opinions.

    Thank you,
    Chris

  2.  

  3. #2
    Member
    Join Date
    Apr 2012
    Posts
    45
    Member #
    31415
    Liked
    13 times
    Sounds pretty spot on to me. Do you have an example of how you handle your forms? That's the only part that doesn't sound like it adheres to the pattern convention.
    Moderator at PHPFreaks

  4. #3
    CPD
    CPD is offline
    Member
    Join Date
    May 2012
    Location
    South London, England
    Posts
    36
    Member #
    31775
    Liked
    5 times
    I'll post an example tomorrow when I have a little more time for you to review. Appreciate the response.

  5. #4
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Sounds good, man, just remember, MVC is just the surface (presentation) layer of your application. There should be (potentially multiple) layers of lower-level functionality upon which it's built. AKA business logic layers.

  6. #5
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Another thing - sounds like you're using htaccess as your Routher. That's fine for smaller projects, but not really a robust manageable implementation. You'll eventually want to develop a Router in your framework to handle the uri parsing, decision making, and task delegation.

    Some major advantages include the ability to define Routes with meta-data and the ability to implement a REST-ful architecture.

  7. #6
    CPD
    CPD is offline
    Member
    Join Date
    May 2012
    Location
    South London, England
    Posts
    36
    Member #
    31775
    Liked
    5 times
    I'm currently using htaccess to make the URIs "look nice" and set a structure for processing it. The processing actually takes place in the PHP.

    Moreover, the model is effectively your business logic is it not? It can use other components but ultimately you can pass all your logic through the model.

  8. #7
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    No, the Model is a Data Access Layer (DAL). It should not contain any business rules in it. The Model should only handle data reading, writing, and validation.

  9. #8
    CPD
    CPD is offline
    Member
    Join Date
    May 2012
    Location
    South London, England
    Posts
    36
    Member #
    31775
    Liked
    5 times
    Okay so when you refer to business logic what exactly do you mean?

  10. #9
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Business logic is anything that is not Data-related or UI related. For example, when I make a post here, there's a process that checks if I'm logged in, checks if I have permission to post here, checks if the thread is open, and all those good things, and acts appropriately based on that (redirecting me or saving the post as necessary). That process uses models to read and write data, but the rules are all business logic. Business Logic is the workflows that define how an application works.

  11. #10
    CPD
    CPD is offline
    Member
    Join Date
    May 2012
    Location
    South London, England
    Posts
    36
    Member #
    31775
    Liked
    5 times
    Oh I understand. I have that separated to a degree. I do however, have methods to manipulate data inside the models at the minute. Are you saying this should be taken out an placed elsewhere? Or are you saying whole methods with no read or write functionality should be taken out? I would assume the latter as the first one could end up making an application over complex with unnecessary files.


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:06 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com