Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    76
    Thanks
    61
    Thanked 0 Times in 0 Posts

    Advice/help with MVC please?

    Hello I am trying to build a model view controller system and am a little stuck on implementing it.

    In my model I have a method in which I pass a query to

    Code:
    SELECT name, age, biog FROM table LIMIT 20
    PHP Code:
      public function searchResults($query) {
        foreach (
    $c->query($query) as $row) {
          
    $data .= "<h1>{$row['name']}</h1>";
          
    $data .= "<p>{$row['age']}<p>";
          
    $data .= "<p class=\"mytext\">{$row['biog']}<p>";
        }
        
    $this->webpage str_replace('{BODY}',$data$this->webpage);  //$this->webpage is my HTML file
       

    And as you can see I capture the data in a foreach loop and then run a str_replace on my view ($this->webpage) to put that data into my template.

    Now this does work, but where it's falling down I think is that in my method I am passing in HTML syntax such as <h1> , <p> and class styles, which should only be in the template (view) I think.

    Can anybody suggest please how I can pass all of this data to my template (view) without the HTML tags (which i can then put in my view) Thank you.
    Last edited by Oatley; 05-15-2013 at 05:08 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Indeed, this is a part of the problem with MVC in a template language like PHP.
    Logically with an MVC, the view would initiate a request to a controller, which polls a model, which (either) returns to the view or responds to the controller which responds to the view. Languages like PHP are designed to interact at both levels and since it's not an event driven language it becomes a bit deficient with regards to the view -> controller invocation. Stack on that HTTP is stateless, then the combinations become a little weird in an MVC paradigm since you won't really need to go from the view to the controller anymore. So really its more of a controller -> model -> view approach in PHP where the controller controls the majority of the entire system instead of the view initiating all controls.

    You could use observable patterns (for which PHP has the built in splObserver and splSubject interfaces, but making your own is probably the most trivial of all design patterns), so that the controller can change things and respond to change type events on the subjects upon receiving notification of said changes. So I'd see a controller as being both an splObserver and splSubject, a model as a splSubject, and a view as a splObserver (or the model and view can also be both). Change in model issues a notify() event, which is picked up by the observer in the controller, which determines which view is in use and set some type of flag which then notifies its observers, which (this is the view now) determines that it's responsible for doing something. The implementation can change of course, you can go view -> controller -> model -> view, or you can go view -> controller -> model -> controller -> view, which is what I would do in a language like Java (not in PHP :P). Both have pros and cons to them, the model -> controller lets me completely decouple views and models so I could link in several views to several models and vice versa without ever needing to modify underlying structure. It does create an absolute mess with collections though, for which model->view is superior in handling.

    IMO, this here:
    PHP Code:
      public function searchResults($query) {
        foreach (
    $c->query($query) as $row) {
          
    $data .= "<h1>{$row['name']}</h1>";
          
    $data .= "<p>{$row['age']}<p>";
          
    $data .= "<p class=\"mytext\">{$row['biog']}<p>";
        }
        
    $this->webpage str_replace('{BODY}',$data$this->webpage);  //$this->webpage is my HTML file
       

    Does NOT belong in a model. This has format output in it which belongs in the view; the model only cares about the data, so the name, age, and biog would be stored in a model, and the view would need to get access to it (which depends on how coupled the system is).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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