尝试理解 MVC。此实施与我的网站有多接近?
对于 Web 编程来说仍然很陌生,我本身并没有什么具体的问题,但我想知道这种设计与在这些条件下如何实现 MVC 有多接近。 这是我的模板类using. db.php
只是连接到数据库。
我想远离“使用 CodeIgniter、CakePHP、Zend Framework 等”之类的答案,因为我很可能最终会这样做,但现在我想了解如何实现准系统 MVC 模式在使用开箱即用的 PHP 和 HTML 的典型网站中。另外,我已经阅读了模型视图控制器的维基百科页面,但我仍然对如何在这种情况下应用它感到困惑。
我不太喜欢我现在使用的解决方案,因为它看起来仍然相当无组织。我不确定我到底是什么意思,但似乎有点味道。由于存在大量变量,它仍然显得相当混乱和不优雅。网站的许多部分似乎都在努力访问其他部分,而且整体设计有点混乱。我不断地将大量变量传递给构造函数,整个解决方案似乎非常不灵活。我不知道;我仍然对控制器在这种情况下是什么或者我是否有一个感到有些困惑。希望有人可以缓解所有这些混乱 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);
}
}
}
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最近自己也经历过这个困境,经过一番尝试和错误后,我得到了以下设计模式实现,它与 ericacm 的答案略有不同:
前端控制器处理所有传入请求并将其委托给所需的控制器。这通常包括以某种方式将传入 URL 映射到文件路径,但您也可能会加载一些初始数据或基类,或者设置一些应用程序环境设置。
一旦控制器被加载,前端控制器就不会再次使用,直到有新的请求为止。 (需要一个更好的词)“主”控制器现在加载任何所需的模型类并调用其中的方法来获取任何所需的数据。
模型具有从数据源(即数据库)提取和处理所请求数据的方法。除了其他模型和数据库连接之外,它无法访问任何内容。
一旦数据被加载,控制器就会加载相关的视图类并将数据“推送”到视图中保存的变量中。
View 类本身没有“更高”的逻辑功能,并且无法访问 Model 或 Controller 方法。它实际上只是提供通过各种
getSomeVar()
或renderSomeData()
方法将数据处理到所需输出媒体(即 html)的方法。最后控制器调用视图的方法来渲染页面,此时控制器的工作就完成了。视图将加载必要的模板文件,这些模板文件将被解释为浏览器生成输出。
模板文件大部分是 HTML 结构,没有编程逻辑。数据仅通过调用视图中的各种
renderSomething()
方法加载到页面中。我的想法是控制器应该控制一切(duh)!
哈
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:
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.
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.
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.
Once the data has been loaded the Controller loads the relevant View class and 'pushes' the data into variables held within the View.
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()
orrenderSomeData()
methods.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.
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
Web 世界中的 MVC 是这样工作的:
采用该基本结构并将其进一步扩展:
这是一个超级基本的 MVC 设置,您可以对其进行扩展。
MVC in the web world works like this:
Taking that basic structure and expanding it a little further:
That is a super basic MVC setup that you can expand on.