doulu6929 2011-04-07 16:01
浏览 35
已采纳

试图了解MVC。 我的网站的实施有多接近?

Very new to web programming still and I don't really have a specific question per se, but I was wondering how close this design is to how an MVC would be implemented under these conditions. Here is the template class that I am using. db.php just connects to the database.

I'd like to stay away from answers like "Use CodeIgniter, CakePHP, Zend Framework, et cetera" because I most likely am going to end up doing that eventually but for now I'd like to understand how to implement a barebones MVC pattern in a typical website using out of the box PHP and HTML. Also, I've read the wikipedia page for Model-view-controller and I am still confused as to how to apply it in this circumstance.

I don't really like the solution I am using right now because it still seems rather unorganized. I'm not sure what exactly I mean but it just seems to smell a bit. With the mass amount of variables it still seems rather messy and inelegant. It seems like many parts of the site struggle for access to other parts and that the overall design is a bit mucky. I am constantly passing large amounts of variables to constructors and the entire solution seems very inflexible. I don't know; I'm still somewhat confused as to what the controller is in this context or whether or not I even have one. Hopefully someone can alleviate all this confusion x_x

index.php

<?php
require('db.php');
require('header_model.php');
require('submissions_model.php');
require('template.php');

$headerModel = new HeaderModel();
$page = $headerModel->getPage();
$sort = $headerModel->getSort();
$search = $headerModel->getSearch();

$submissionsModel = new SubmissionsModel($sort, $page, $resultsPerPage, $search);
$submissions = $submissionsModel->getSubmissions();
$outcomeCount = $submissionsModel->getOutcomeCount();

$index_view = new Template('index_view.php', array(
    'header' => new Template('header.php'),
    'menu' => new Template('menu.php', array('sort' => $sort)),
    'submissions' => new Template('submissions.php', array('submissions' => $submissions)),
    'pagination' => new Template('pagination.php', array('page' => $page, 'resultsPerPage' => $resultsPerPage, 'outcomeCount' => $outcomeCount, 'sort' => $sort)),
    'footer' => new Template('footer.php')
));

$index_view->render();
?>

index_view.php snippet

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>My Website</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <link href="styles.css" rel="stylesheet" type="text/css"/>
        <link href="favicon.png" rel="shortcut icon" />
    </head>
    <body>
        <?php
        $this->header->render();
        $this->menu->render();
        $this->submissions->render();
        $this->pagination->render();
        $this->footer->render();
        ?>
    </body>
</html>

header_model.php snippet

public function getSearch() {
    if (isset($_GET['search']))
        return $_GET['search'];
    else
        return '';
}

submissions_model.php snippet

class SubmissionsModel {
    private $sort;
    private $page;
    private $resultsPerPage;
    private $search;

    public $submissions;
    public $outcomeCount;

    public function __construct($sort, $page, $resultsPerPage, $search) {
        $this->sort = $sort;
        $this->page = $page;
        $this->resultsPerPage = $resultsPerPage;
        $this->search = $search;

        $this->initialize();
    }

    private function initialize() {
        $submissionQuery = $this->getSubmissionQuery($this->search);
        $this->outcomeCount = mysql_num_rows($submissionQuery);
        $this->submissions = array();

        while ($row = mysql_fetch_assoc($submissionQuery)) {    
            $voteblock = $this->getVoteblock($row);
            $tags = $this->getTags($row);
            $commentCount = mysql_num_rows(mysql_query("SELECT id FROM comments WHERE submissionID = $row[id]"));
            $this->submissions[] = array('submission' => $row, 'upvote' => $voteblock['upvote'], 'votes' => $voteblock['votes'], 'downvote' => $voteblock['downvote'], 'tags' => $tags, 'commentCount' => $commentCount);
        }
    }
}
  • 写回答

2条回答 默认 最新

  • dtyz76562 2011-08-15 11:33
    关注

    I've recently been through this quandary myself and after some trial and error came to the following design pattern implementation which differs slightly from ericacm's answer:

    1. A Front Controller handles all incoming requests and delegates out to the required Controller. This usually consists of mapping the incoming URL to a file path somehow, but you may also be loading some initial data or base class, or setting up some application environment settings.

    2. Once the Controller is loaded the Front Controller isn't used again until there is a new request. The (for want of a better word) 'Main' Controller now loads any required Model class(es) and calls methods in them to obtain any required data.

    3. The Model has methods to extract and process the requested data from the data sources (i.e. a database). It cannot access anything except for other models and the DB connection.

    4. Once the data has been loaded the Controller loads the relevant View class and 'pushes' the data into variables held within the View.

    5. The View class itself has no 'higher' logic functions and has no access to the Model or Controller methods. It is really only there to provide methods for processing data into the required output media (i.e. html) through various getSomeVar() or renderSomeData() methods.

    6. Finally the Controller calls the View's method to render the page, at which point the Controllers job is done. The View will load the necessary template files which will be interpreted to produce the output for the browser.

    7. The template file(s) are mostly structure HTML with no programming logic. The data is only loaded into the page via calls to various renderSomething() methods within the View.


    My thinking behind this was that the Controller should be in control (duh), of everything!

    hth

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站