有人能够提供一个使用MVC方法编写PHP Hello World的非常简单的示例吗?
有人能够提供一个使用MVC方法编写PHP Hello World的非常简单的示例吗?
这里有一些“Hello, World”MVC示例:
function get_users() {
return array(
'Foo',
'Bar',
'Baz',
);
}
function users_template($users) {
$html = '<ul>';
foreach ($users as $user) {
$html .= "<li>$user</li>";
}
$html .= '</ul>';
return $html;
}
function list_users() {
$users = get_users();
echo users_template($users);
}
主要思想是将数据访问(模型)与数据呈现(视图)分开。控制器只需负责将两者连接起来,不做其他工作。
以下是最基本的示例。您的index.php文件作为控制器,从模型获取一些数据,然后通过视图文件包含HTML。
/* index.php?section=articles&id=3 */
// includes functions for getting data from database
include 'model.php';
$section = $_GET['section'];
$id = $_GET['id'];
switch ( $section )
{
case 'articles':
$article = getArticle( $id );
include 'article.view.php';
}
.
/* article.view.php */
<html>
<head>
<title><?=$article['title']?></title>
</head>
<body>
<h1><?=$article['title']?></h1>
<p><?=$article['intro']?></p>
<?=$article['content']?>
</body>
</html>