我对模型- View - Controller 模式有一些(也许是基本的)知识,我想使用它创建一个站点。但是我发现如何实际实现它有点令人困惑。我陷入了细节。
假设我有一个网站,每个用户都保留一些待办事项列表。你会如何处理这个问题? 你会创建什么类?哪个类将输出 HTML,哪个类将充当 Controller 以及它如何与 View 通信以产生输出?等
抱歉,如果它看起来很傻,我想它一定有点简单,但我被卡住了。
最佳答案
备案:
在 PHP 中做一个 MVC 并不难,它更多地与纪律有关,而不是一项困难的任务。
a) 模型(可选,您可以在 PHP 中使用数组)
<?php
class MyModel() {
}
?>
b) 路由(index.php?)
<?php
include "...";
// here we collects all the information, such post,get and path values
$action=...;
$param=....;
switch($controller) {
case "my": // www.myweb.com/my/action
include "controller\MyController.php"; // open the right controller.
break;
}
?>
c) Controller
<?php
include "model\MyModel.php";
switch($action) {
case "add":
// here live the logic, information, call for services and such.
$model=....;
// and finally...
include "view\MyView.php";
break;
}
?>
d) 查看
<html>
<body>
<?=$model->field;?>
</body>
<html>
注意:
a) View 应该尽可能干净。认为该 View 可以由不关心 php 的网页设计师创建。
b) View 始终是流程的最后一步。 View 网络始终不返回任何内容。
关于php - 如何在不使用某些框架的情况下在 PHP 中使用 MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7308361/