Ion*_*tan 34
这是一些"Hello,World"MVC:
function get_users() {
return array(
'Foo',
'Bar',
'Baz',
);
}
Run Code Online (Sandbox Code Playgroud)
function users_template($users) {
$html = '<ul>';
foreach ($users as $user) {
$html .= "<li>$user</li>";
}
$html .= '</ul>';
return $html;
}
Run Code Online (Sandbox Code Playgroud)
function list_users() {
$users = get_users();
echo users_template($users);
}
Run Code Online (Sandbox Code Playgroud)
主要思想是将数据访问(模型)与数据表示(视图)分开.控制器应该只是将两者连接在一起.
Dis*_*oat 11
这是最基本的例子.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';
}
Run Code Online (Sandbox Code Playgroud)
.
/* article.view.php */
<html>
<head>
<title><?=$article['title']?></title>
</head>
<body>
<h1><?=$article['title']?></h1>
<p><?=$article['intro']?></p>
<?=$article['content']?>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Zend Framework的快速入门是一个不错的"简单应用程序"的例子(不是"Hello World",但不是更多 - 使用MVC作为"Hello World"应用程序有点像使用核弹杀死一个bug),基于Zend Framework,并使用MVC.
之后,如果你想更进一步,你可以看一下电子书Survive The Deep End! - 仍在进行中,但无论如何都是一个有趣的阅读.
这与ZF有关; 我想你可以找到与Symfony或CakePHP等其他MVC框架相同的东西.