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 7 1 2 3 ... LastLast
Results 1 to 10 of 61
  1. #1
    Senior Member Zboost's Avatar
    Join Date
    Apr 2011
    Location
    Houston
    Posts
    246
    Member #
    27459
    Liked
    20 times
    So, I have been interested in learning MVC Frameworks, and I am the kind of person that HAS to know how something works. I know there are tons of different frameworks out there; some big, and some small. I want to be able to be able to start something for myself for projects, so I know how it works inside and out, and to build on as sort of a ongoing way to develop skills.

    That being said, I have read every damn tutorial I can get a hold of, I bought books on pattern designs, and I just really am not understanding how the hell this works! Im just looking for some collaborative discussion to help me understand what ever it is I am not understanding. I have never had such a hard time learning something. I also want this to be something others can search for if they have issues that may be able to help them understand how it works.

    Here is what I have gathered, you start out with a directory structure, splitting the responsibilities of the code.

    Something basic, I don't know if there is a certain rule to this design or not, but this is just the different folders I have. I am just basing this off of what I have seen and trying to figure this all out.

    site/
    site/index.php
    site/.htaccess

    site/application/
    site/application/MainController.php

    site/application/models/
    site/application/models/Home.php

    site/application/views/
    site/application/views/Home.php

    site/application/controllers/
    site/application/controllers/Home.php

    site/configuration/
    site/configuration/Configuration.php

    The user goes to www . yoursite . com, and the index.php defines variables, gets settings from configuration, and loads the default controller.
    The controller will then determine which routes to use, which models and views to call, then calls them.

    .htaccess cleans your URLs and forces the users always to index.php.

    Now this is just my super basic explanation of this, what I get confused on is the controller and routing. When the controller comes up it what information is it using to pull models and views? Also after it determines where to route the user, the user may click a link to Links.php page. How does it go back through the proccess and know where to route the user next?

    I am sorry if these questions seem simple, and I am sure that I am making this harder then it really is but I cannot for the life of me get it.

  2.  

  3. #2
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    The URL path is interpreted by the "core" to point to a given Controller's action. A Controller is a special type of Class whose methods are defined as actions... and so a Controller, therefore, is meant to define related pieces of a website as a single collective of actions, e.g.

    PHP Code:
    class UserController {
        public function 
    login();
        public function 
    logout();
        public function 
    dashboard();
        public function 
    profile($id);
        public function 
    blog_posts($id);

    You may define "user" => UserController as a route in your config, which would then interpret all paths beginning with example.com/user/ to go to the user controller, e.g.

    example.com/user/login => UserController::login();

    The logic of your application is contained mostly within those actions, so you may have something like this... you can see how Models and Views get used within your controller to separate the logic.

    PHP Code:
    class UserController {
        public function 
    login() {
            if (isset(
    $this->data) {
                
    // Form was posted
                
    $users UserModel::findBy(array(
                    
    "username" => $this->data["username"],
                    
    "password" => $this->data["password"]
                );
                if (empty(
    $users)) {
                    
    // User not found - redirect to login form
                    
    $this->setAsynchronousMessage("User not found!");
                    
    HttpResponse::redirect("/users/login_form");
                } else {
                    
    // Great success!!!! Redirect to dashboard
                    
    $this->setAsynchronousMessage("Welcome!");
                    
    HttpResponse::redirect("/users/dashboard");
                }
            } else {
                
    // Form not posted, redirect to login form
                
    $this->setAsynchronousMessage("You didn't post anything!");
                
    HttpResponse::redirect("/users/login_form");
            }
        }

        public function 
    login_form() {
            
    // Render a login form in a view
            
    if (!empty($this->data)) $form_data $this->data;
            
    $this->render("login_form_view", array("form_data" => $form_data));
        }


  4. #3
    Senior Member Zboost's Avatar
    Join Date
    Apr 2011
    Location
    Houston
    Posts
    246
    Member #
    27459
    Liked
    20 times
    I wish I could say I understood far enough in for that to help me understand, I cannot believe I am having such a hard time with this.

  5. #4
    Senior Member Zboost's Avatar
    Join Date
    Apr 2011
    Location
    Houston
    Posts
    246
    Member #
    27459
    Liked
    20 times
    I did some more searching and I think what is getting me is understanding how to use front-controller or page-controller.

  6. #5
    Senior Member Ganners's Avatar
    Join Date
    Feb 2011
    Location
    United Kingdom
    Posts
    415
    Member #
    27007
    Liked
    92 times
    Not sure what you mean by front controller and page controller. This is all dependant on how your framework is laid out. Personally, I split the application up into folders which I call components, then inside each component there is an MVC. So your page controller is a front controller if you like.

    If I have misunderstood then perhaps you're thinking about when you will use a MainController as you have described. You'll probably still only call your front controller but use inheritance to extend your MainController and keep it's behaviour.
    Mark Gannaway Software Developer

    Recent Experiments
    - Backpropogation Neural Network language solving (http://ann.ganners.co.uk/)
    - Animated image to ASCII (http://google.ganners.co.uk/)
    - 3D Paper Characters (http://cybergame.ganners.co.uk/)
    - Anagram solving (http://roflol.co.uk/)

  7. #6
    Senior Member Zboost's Avatar
    Join Date
    Apr 2011
    Location
    Houston
    Posts
    246
    Member #
    27459
    Liked
    20 times
    I just read there are different types of controllers for routing; front, page, and hierarchy. Each routes a little differently.

  8. #7
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Sounds like something specific to the framework you're planning to use.

  9. #8
    Senior Member Zboost's Avatar
    Join Date
    Apr 2011
    Location
    Houston
    Posts
    246
    Member #
    27459
    Liked
    20 times
    I would think the way the framework route paths could be specific to the framework, I just can't understand the routing concept, I hate my life lol. I don't understand how it changes the physical url to get different controllers and actions using most methods I see people use? Like you posted above smoseley.

    /site/application/controller/action

  10. #9
    Senior Member Ganners's Avatar
    Join Date
    Feb 2011
    Location
    United Kingdom
    Posts
    415
    Member #
    27007
    Liked
    92 times
    How it changes the URL? There's not much magic involved, the router will just split the URL up into it's segments, then match the first segment against a controller with the matching name, then the second segment against an action with the matching name.

    I don't understand the:
    I don't understand how it changes the physical url to get different controllers and actions using most methods I see people use?
    There is nothing which changes the physical URL. You can use url rewriting engines to change the URL to make it prettier, but most routing these days is done with splitting up the URL into segments and searching the file system for matching controllers/actions.
    Mark Gannaway Software Developer

    Recent Experiments
    - Backpropogation Neural Network language solving (http://ann.ganners.co.uk/)
    - Animated image to ASCII (http://google.ganners.co.uk/)
    - 3D Paper Characters (http://cybergame.ganners.co.uk/)
    - Anagram solving (http://roflol.co.uk/)

  11. #10
    Senior Member Zboost's Avatar
    Join Date
    Apr 2011
    Location
    Houston
    Posts
    246
    Member #
    27459
    Liked
    20 times
    So then, when I load my default view, my url will say,

    /site/application/controller/

    when my controller loads. When my view loads and I click a link it starts over and goes to links.php, it is still going to say, site/application/controller/


Page 1 of 7 1 2 3 ... 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 11:58 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com