ad

Wednesday, 22 April 2015

Model, View and Controller - with example

What we've been told since the childhood is if you put things at the right place, it's easier to find later on and everyone can do their work without having problems, and that's what MVC essentially is, MVC it's a design pattern, it says, keep everything seperately, model - view - controller.

Model is like storage, it interacts with databases, stores files etc.. Database developer or admin work with it.
View is whatever you see on webpage, design, interface etc.. web designer or front end developers usually work with it.
Controller is where all your logic goes if statements, loops functions etc.. developer/programmer work with with it.

VIEW:: Lets use view, show user interface.. input boxes etc..
CONTROLLER:: Lets gets data to controller POST/GET.
CONTROLLER:: Lets do logic, validate it etc..
CONTROLLER:: Lets now call model to validate(see if credentials are right or not/or store data etc..) the data.
MODEL::Lets check data against database.
CONTROLLER:: Lets check what model has returned.
CONTROLLER:: Lets do action depending on what model has returned.
VIEW:: I'm waiting for controller to reply back.
VIEW:: I will show interface/graphics/text depending on what controller has replied back. 

Index
include("login_view.php");
include("login_model.php");
include("login_controller.php");

Login_view::show_login_form();
$Login_controller = new Login_controller();
$Login_controller->validate($conn);


Model(Interacting with database trying to match information against information in tables)
    class Login_model{
        function __construct($conn){
            $this->conn = $conn;
        }
        function check_user_and_pass($username, $password){
            $select_user_pass_query = $this->conn->prepare("SELECT username, password FROM users WHERE username = :username, password = :password");
           
            if($select_user_pass_query->execute(array(':username'=>$username, ':password'=>$password))){
                $details = $select_user_pass_query->fetch(PDO::FETCH_NUM);
                if(!empty($details)){
                    return true;
                }else{
                    Login_view::show_message("Wrong user or password");
                }
            }else{
                Login_view::show_message("Error occured");
            }
        }
    }


View(Showing the form)
class Login_view{
    static function show_login_form(){
        echo
        "<form method=\"GET\" action=\"{$_SERVER["PHP_SELF"]}\">
            Username: <input name=\"username\" type=\"text\"> </br>
            Password: <input name=\"password\" type=\"password\"> </br>
            <input name=\"login\" type=\"submit\" value=\"Login\">
        </form>";
    }
   
    static function show_message($message){
        echo $message;
    }
}


Controller(doing all the main logic that's needed to be done before model starts his job - ignored the check of session whether user is logged in, view is usually invoked if controller decides to. For example, it could say, user is logged it do not show the form.)
    class Login_controller{
        function validate($conn){
            if( isset($_GET["username"]) && isset($_GET["password"]) ){
                if( !empty($_GET["username"]) and !empty($_GET["password"]) ){
                    $Login_model = new Login_model($conn);
                    if($Login_model->check_user_and_pass($_GET["username"], $_GET["password"])){
                        session_start();
                        $_SESSION["username"] = "is_logged_in";
                        Login_view::show_message("logged in");
                    }
                }else{
                    Login_view::show_message("empty");
                }
            }
        }
    }

No comments:

Post a Comment