SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2012
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MVC Pattern Implementation

    Okay i'm trying to learn how to make an MVC. I quickly found out reading up on the concept and trying to put it in your code are two very different things. So far i have this idea:
    When the user goes to a page (website.com/index), the controller is activated and checks the url for a query string. depending on the query string (foo.com/view/action/param/param...) it grabs the view (contents of the page) from the view class and spits it out.
    When the user wants to log in or something, they would click a link that would go to website.com/view/login and i guess the view would pass parameters to the controller and the controllers sends that to the model, the model validates everything and sends back either an error message or logs the user in, the controller gets that response and sends it back to the view. Is this how it's supposed to work? And if so, how could you impliment this in code? I would really like to see an example if possible

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,277
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    First, an obligatory explanation:

    The original definition of MVC was written with desktop applications in mind, and it doesn't translate perfectly to stateless, server-side web applications. For example, MVC controllers are supposed to handle user input -- such as mouse clicks, key presses, touches, gestures, device orientation, etc -- and translate that input into commands for the view and the model. But in server-side web applications, there's only one input: the HTTP request. Also, MVC views are supposed to be able to react to user input as well as to changes in the model, but in server-side web applications, there won't be any new user input for the view to react to, nor will there be any changes in the model that would require the view to update itself.

    Because of these differences, MVC has typically been implemented differently for the web. Views became templates, and the templates don't access the model directly. Instead they are fed data by the controller, which acts as a mediator between the model and the view. It turns out there's another design pattern that more accurately reflects the way server-side web applications typically work, called Presentation–abstraction–control.

    With that explanation out of the way, I think we need to know which "kind" of MVC you're trying to implement before we can help. If you'd like to implement MVC in its original, purest sense, then @TomB ; can help you with that. Or if you'd like to implement the kind of MVC that today's popular frameworks are built on, then I and some others here can help you with that too.
    "First make it work. Then make it better."

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2012
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here is an article i read that had the typical different types:
    http://stackoverflow.com/questions/7...622038#7622038
    the one i want to use is the MPV pattern

  4. #4
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,277
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Gotcha. In that case, I recommend the article From Flat PHP to Symfony2. This article starts with a traditional, flat PHP script, and it takes you step-by-step to reorganize it into MVC (or at least what frameworks such as Zend and Symfony consider to be MVC, which we now call MVP/PAC).
    "First make it work. Then make it better."

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2012
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you! I will take a look at this article.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2012
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have looked at the article and started to try my hand at implementing a version of MVC. I want to ask a few questions though..
    First, i have a class that will try to get the ball rolling, but the autoloader won't work. Here is the code:

    PHP Code:
    <?php
    class Bootstrap{
        private 
    $url
        private 
    $basePath;

        public function 
    __construct(){
            
    $basePath realpath(dirname(__FILE__)) ;
            
    $basePath .= "\\";
            
    $this->basePath $basePath;
        }


        public function 
    __autoload($class_name) {
                include  
    $this->basePath$class_name '.php';
            }

        public function 
    startApp(){

            
    $url $_GET['url'];
            
    $url explode("/"$url);
            
    // print_r($url);            

            // links: view/action/param1/param2
            
    $view = new View();
            
    $view->render($url[0]);
            
    $controller = new Controller();    
        }
    }
    All of the classes i want to load are kept in the same folder..
    Also, a question about the way information is handled. I see others making a controller and a view for each page, but wouldn't it be better if there was one controller for each action that was going to happen? And having a template for each page and just including with a render method from the view with the page name passed as a variable?

  7. #7
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    351
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    You will have to register your autoload method using spl_autoload_register to get it to work.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2012
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah thank you. I got the autoloader to work.
    Could i get some advice on the functionality?

  9. #9
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    351
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    The functionality for what? The bootstrap class? Id say it looks good so far, although once your routes become more complicated than the simple pattern controller/action/params you may need a router class to handle url requests and a dispatcher to convert them into the appropriate form for your controller class to use.


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
  •