Mô hình MVC trong PHP

Thảo luận trong 'Lập trình PHP' bắt đầu bởi Boss, 7/1/15.

Đã xem: 884

  1. Boss Moderator

    Mô hình MVC trong PHP

    MVC = Model - View - Controller = Truy vấn - Nhập & Hiển Thị - Phần còn lại của 2 cái kia Việc chính yếu trong sử dụng MVC là để tách biệt các phần trong chương trình của mình.


    Ví dụ cho mô hình bình thường Code:

    <?php
    $connect = mysql_connect('myserver', 'mylogin', 'mypassword');
    mysql_select_db('myDB');
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $news_id = $_POST['news_id'];
    mysql_query("INSERT INTO commentaires SET news_id='$news_id',
    auteur='".mysql_escape_string($_POST['auteur'])."',
    texte='".mysql_escape_string($_POST['texte'])."',
    date=NOW()"
    );
    header("location: ".$_SERVER['PHP_SELF']."?news_id=$news_id");
    exit;
    } else {
    $news_id = $_GET['news_id'];
    }
    ?> <html> <head> <title>Les news</title> </head> <body> <h1>Les news</h1> <div id="news"> <?php
    $news_req = mysql_query("SELECT * FROM news WHERE id='$news_id'");
    $news = mysql_fetch_array($news_req);
    ?> <h2><?php echo $news['titre'] ?> postée le <?php echo $news['date'] ?></h2> <p><?php echo $news['texte_nouvelle'] ?> </p> <?php
    $comment_req = mysql_query("SELECT * FROM commentaires WHERE news_id='$news_id'");
    $nbre_comment = mysql_num_rows($comment_req);
    ?> <h3><?php echo $nbre_comment ?> commentaires relatifs à cette nouvelle</h3> <?php while ($comment = mysql_fetch_array($comment_req)) {?> <h3><?php echo $comment['auteur'] ?> a écrit le <?php echo $comment['date'] ?></h3> <p><?php echo $comment['texte'] ?></p> <?php } ?> <form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>" name="ajoutcomment"> <input type="hidden" name="news_id" value="<?php echo $news_id?>"> <input type="text" name="auteur" value="Votre nom"><br /> <textarea name="texte" rows="5" cols="10">Saisissez votre commentaire</textarea><br /> <input type="submit" name="submit" value="Envoyer"> </form> </div> </body> </html>

    Nếu làm theo mô hình MVC, thì code phía trên sẽ được chia làm 3 file khác nhau .

    1 là file mymodel.php dùng để xử lý thông tin trong database

    Code:

    <?php
    function dbconnect()
    {
    static $connect = null;
    if ($connect === null) {
    $connect = mysql_connect('myserver', 'mylogin', 'mypassword');
    mysql_select_db('myDB');
    }
    return $connect;
    }

    function get_news($id)
    {
    $news_req = mysql_query("SELECT * FROM news WHERE id='$news_id'",dbconnect());
    return mysql_fetch_array($news_req);
    }

    function get_comment($news_id)
    {
    $comment_req = mysql_query("SELECT * FROM commentaires WHERE news_id='$news_id'",dbconnect());
    $result = array();
    while ($comment = mysql_fetch_array($comment_req)) {
    $result[] = $comment;
    }
    return $result;
    }

    function insert_comment($comment)
    {
    mysql_query("INSERT INTO commentaires SET news_id='{$comment['news_id']}',
    auteur='".mysql_real_escape_string($comment['auteur'])."',
    texte='".mysql_real_escape_string($comment['texte'])."',
    date=NOW()"
    ,dbconnect() );
    }

    2 là file myview.php chứa phần html hiển thị

    Code:
    <html> <head> <title>Les news</title> </head> <body> <h1>Les news</h1> <div id="news"> <h2><?php echo $news['titre'] ?> postée le <?php echo $news['date'] ?></h2> <p><?php echo $news['texte_nouvelle'] ?> </p> <h3><?php echo $nbre_comment ?> commentaires relatifs à cette nouvelle</h3> <?php foreach ($comments AS $comment) {?> <h3><?php echo $comment['auteur'] ?> a écrit le <?php echo $comment['date'] ?></h3> <p><?php echo $comment['texte'] ?></p> <?php } ?> <form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>" name="ajoutcomment"> <input type="hidden" name="news_id" value="<?php echo $news_id?>"> <input type="text" name="auteur" value="Votre nom"><br /> <textarea name="texte" rows="5" cols="10">Saisissez votre commentaire</textarea><br /> <input type="submit" name="submit" value="Envoyer"> </form> </div> </body> </html>

    3 là file mycontroller.php để xử lý vấn đề

    Code:
    <?php
    require ('mymodel.php');
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    insert_comment($_POST);
    header("HTTP/1.1 301 Moved Permanently");
    header("location: {$_SERVER['PHP_SELF']}?news_id={$_POST['news_id']}");
    exit;
    } else {
    $news = get_news($_GET['news_id']);
    $comments = get_comments($_GET['news_id']);
    require ('myview.php');
    }
    ?>

    Xong...đó là mô hình MVC đơn giản nhất .. có thể thấy rõ việc tách biệt các loại thông tin cần xử lý để có thể dễ dàng can thiệp vào từng phần khác nhau của chương trình
     
    Đang tải...
    nam lim xanh

    Bình Luận Bằng Facebook